function declaration with default parameter

M

Matthias Pfeifer

Hi there,

I am trying to declare a function that takes a std::list<> parameter.
I want this function to have an empty list as a default parameter.
It's a template function also. Currently i am stuck because my
compiler does not find a matching function when i do a call.

here is my code:

// ############## code begins
#include <list>

using namespace std;

template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T > > =list<pair<int, int > >() );

int main (int argc, char* argv)
{
int* c, t, i;

int* result = f(c, t, i);
}

template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T > > )
{
return (int *) 0;
}
// ############## code ends

compiler says

testcase.cpp: In function 'int main(int, char*)':
testcase.cpp:16: error: no matching function for call to 'f(int*&, int&,
int&)'

compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
here. Would be nice if someone knows how to do this right... thanks in
advance.

matthias
 
A

Amal P

Hi there,

I am trying to declare a function that takes a std::list<> parameter.
I want this function to have an empty list as a default parameter.
It's a template function also. Currently i am stuck because my
compiler does not find a matching function when i do a call.

here is my code:

// ############## code begins
#include <list>

using namespace std;

template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T > > =list<pair<int, int > >() );

int main (int argc, char* argv)
{
int* c, t, i;

int* result = f(c, t, i);

}

template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T > > )
{
return (int *) 0;}

// ############## code ends

compiler says

testcase.cpp: In function 'int main(int, char*)':
testcase.cpp:16: error: no matching function for call to 'f(int*&, int&,
int&)'

compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
here. Would be nice if someone knows how to do this right... thanks in
advance.

matthias

Dear Matthias,

The reason for the error is you didnt specify the type.

A small modification can correct this problem.

I just specified the type also before calling the function.

#include <list>

using namespace std;

template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T > > =list<pair<int, int > >() );

int main (int argc, char* argv)
{
int* c, *t, *i;

int* result = f<int>(c, t, i);

}

template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T > > )
{
return (int *) 0;
}


Thanks and regards,
Amal P.
 
A

Amal P

Dear Matthias,

The reason for the error is you didnt specify the type.

A small modification can correct this problem.

I just specified the type also before calling the function.

#include <list>

using namespace std;

template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T > > =list<pair<int, int > >() );

int main (int argc, char* argv)
{
int* c, *t, *i;

int* result = f<int>(c, t, i);

}

template<typename T>
int* f(const int* c, const int* t, const int* i,
list<pair<int, T > > )
{
return (int *) 0;

}

Thanks and regards,
Amal P.

Dear Matthias,

I hope you write this code just to test the default parameter.
It wont be possible to run this code now also because none of the
pointers point to a valid memory.

Thanks and regards,
Amal P.
 
M

Matthias Pfeifer

Dear Matthias,
I hope you write this code just to test the default parameter.
It wont be possible to run this code now also because none of the
pointers point to a valid memory.

Thanks and regards,
Amal P.

Hi Amal,

you are right. The code was just for demonstration. Thank you for
your accurate answer. My program works now. Btw: Do you know why
it is not possible to give a default parameter to the template parameter?

template<typename T=int> ...

would look nice for me.

Matthias
 
A

Amal P

Hi Amal,

you are right. The code was just for demonstration. Thank you for
your accurate answer. My program works now. Btw: Do you know why
it is not possible to give a default parameter to the template parameter?

template<typename T=int> ...

would look nice for me.

Matthias

Dear Matthias,

As you know template is a mechanism which we can use for easiness
to support different datatype, if you always want it as an int, why
you still use a template?
You should not give like that because it does not worth. If always
you are giving an int there is there any use for using a template
there?

I am sorry if i have mistaken your question.

Thanks and regards,
Amal P.
 
D

dasjotre

#include <list>

using namespace std;

template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T > > =list<pair<int, int > >() );

int main (int argc, char* argv)

either
int main(int argc, char* argv[])
or
int main()
{
int* c, t, i;

int* result = f(c, t, i);

c is a pointer to int but both t and i are just ints.

also

int* c, *t, *i;
int* result = f<int>(c, t, i);

will work but but only for int

int* result = f<char>(c, t, i);

will not, since you can not convert
pair<int,char> to pair<int, int>

one way would be to provide template
version

template<typename T>
int* f(const int*,
const int*,
const int*,
list<pair<int, T > > = list<pair<int, T > >() );

and a non template version for int

int* f(const int*,
const int*,
const int*,
list<pair<int, int > > = list<pair<int, int > >() );

and then call them as
int* result = f(c, t, i); // calls int version
int* result1 = f<char>(c, t, i); // calls template version

your compiler might allow it, but main should
return a value

regards

DS
 
D

dasjotre

your compiler might allow it, but main should
return a value

I meant, your compiler might allow you to
omit return statement, but all functions
declared as returning anything but void
must have a return statement on all
possible execution paths.

DS
 
G

Gavin Deane

Your compiler must allow the omission of the return statement it in
the specific case of main.
I meant, your compiler might allow you to
omit return statement, but all functions
declared as returning anything but void
must have a return statement on all
possible execution paths.

There is an explicit exclusion for main(). If you reach the end of the
function and there is no return statement, the compiler assumes return
0;

Gavin Deane
 
M

Marcus Kwok

dasjotre said:
I meant, your compiler might allow you to
omit return statement, but all functions
declared as returning anything but void
must have a return statement on all
possible execution paths.

main() is special, in that the Standard explicitly allows the return
statement to be omitted, with the result that it will implicitly return
0 to the environment.
 
S

Sylvester Hesp

Matthias said:
Hi Amal,

you are right. The code was just for demonstration. Thank you for
your accurate answer. My program works now. Btw: Do you know why
it is not possible to give a default parameter to the template parameter?

template<typename T=int> ...

would look nice for me.

Matthias

The committee agrees with you and the next C++ revision will allow that
;). For now, you can use an overload to surcome the need to specify the
template argument:

int *f(const int* a, const int* b, const int* c)
{
return f(a,b,c,list<pair<int,int> >());
}

- Sylvester
 
D

dasjotre

main() is special, in that the Standard explicitly allows the return
statement to be omitted, with the result that it will implicitly return
0 to the environment.

you're absolutely right, I can't think of reason why it should
be so thou. can you?

regards

DS
 
M

Marcus Kwok

dasjotre said:
you're absolutely right, I can't think of reason why it should
be so thou. can you?

I can't say for certain (maybe you should ask on comp.std.c++), but my
intuition would be to help make non-conformant code using

void main() { /* ... */ }

legal just by changing it to

int main() { /* ... */ }

But then, if you're changing the return type from "void" to "int", it's
not that much extra work just to add a "return 0;" to the bottom either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top