469,647 Members | 1,755 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

template function with default parameter value

Hi NG,

I am trying to get the attached piece of code to work, but I can't
figure out what I'm doing wrong. To me it seems that when I don't pass
an argument to x::do_something, it should use the default value, which
is always_true().

but gcc says: no matching function for call to `x::do_something()'
and msvs says: could not deduce template argument for '_Tp'

So I see what the problem is, but I don't understand it.
Is it possible what I'm trying to do or not?

Thanks,
Mark
#include <iostream>
using namespace std;

struct always_true {
bool operator()() const { return true; }
};

struct always_false {
bool operator()() const { return false; }
};

class x
{
public:
template<typename _Tp>
bool do_something(const _Tp & op = always_true())
{
return op();
}
//bool do_something() { return false; }
};

int main()
{
x classX;

bool rc = classX.do_something(always_true()); /*OK*/
cout << (rc ? "TRUE" : "FALSE") << endl;

rc = classX.do_something(); /*compile error*/
cout << (rc ? "TRUE" : "FALSE") << endl;

return 0;
}
Jul 23 '05 #1
3 2507
Capstar wrote:
I am trying to get the attached piece of code to work, but I can't
figure out what I'm doing wrong. To me it seems that when I don't pass
an argument to x::do_something, it should use the default value, which
is always_true().

but gcc says: no matching function for call to `x::do_something()'
and msvs says: could not deduce template argument for '_Tp'

So I see what the problem is, but I don't understand it.
What exactly do you not understand? The compiler sees "()" in the call.
It tries to find a function that takes no arguments. To determine the
set of _viable_ functions, it needs the full function type known. You
have the only one there: the template. In order to make this a viable
function, it needs to determine the template argument. It has nothing
between the parentheses to determine the '_Tp'. And the default argument
has no bearing on the process of deduction of the template argument.
Is it possible what I'm trying to do or not?
No, not really. You could use a proxy class template for which you can
provide the default template argument, but it's probably more work than
you want.

Just overload your function.

Thanks,
Mark
#include <iostream>
using namespace std;

struct always_true {
bool operator()() const { return true; }
};

struct always_false {
bool operator()() const { return false; }
};

class x
{
public:
template<typename _Tp>
bool do_something(const _Tp & op = always_true())
Drop the default argument here.
{
return op();
}
//bool do_something() { return false; }
Uncomment this and change the definition to { return always_true()(); }

It will do exactly the same thing as before (and not necessarily the same
as 'return true', since the call will be forwarded to 'always_true's
function call operator).
};

int main()
{
x classX;

bool rc = classX.do_something(always_true()); /*OK*/
cout << (rc ? "TRUE" : "FALSE") << endl;

rc = classX.do_something(); /*compile error*/
cout << (rc ? "TRUE" : "FALSE") << endl;

return 0;
}


V
Jul 23 '05 #2
"Capstar" <ne**@ge.homeip.net> wrote in message
news:d9**********@domitilla.aioe.org...
So I see what the problem is, but I don't understand it.
Is it possible what I'm trying to do or not?

Also see this passage in 14.8.2.4 Deducing template arguments from a type

17A template type-parameter cannot be deduced from the type of a func-
tion default argument. [Example:
template <class T> void f(T = 5, T = 7);
void g()
{
f(1); // OK: call f<int>(1,7)
f(); // error: cannot deduce T
f<int>(); // OK: call f<int>(5,7)
}
--end example]
--
Regards,

Ferdi Smit
smit xs4all nl
Jul 23 '05 #3
Ferdi Smit wrote:
"Capstar" <ne**@ge.homeip.net> wrote in message
news:d9**********@domitilla.aioe.org...
So I see what the problem is, but I don't understand it.
Is it possible what I'm trying to do or not?


Also see this passage in 14.8.2.4 Deducing template arguments from a type

17A template type-parameter cannot be deduced from the type of a func-
tion default argument. [Example:
template <class T> void f(T = 5, T = 7);
void g()
{
f(1); // OK: call f<int>(1,7)
f(); // error: cannot deduce T
f<int>(); // OK: call f<int>(5,7)
}
--end example]


Thanks to both of you. The Victors solution work fine for me.

Mark
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Kevin Dean | last post: by
5 posts views Thread by krishnaroskin | last post: by
5 posts views Thread by Wayne Shu | last post: by
4 posts views Thread by David Sanders | last post: by
7 posts views Thread by neelsmail | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.