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;
}