Hi all,
I am trying to enable only two version of a function. The caller may choose
one
of the two versions by specifying a template parameter. I've got two
versions
for the problem, both of them failing the same way.
--------------------------
Version A): integral template parameter
--------------------------
#include <iostream>
#include
#include
using namespace std;
using namespace boost;
template
enable_if_c
f(T t)
{
cout << 1 << endl;
}
template
enable_if_c
f(T t)
{
cout << 2 << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
f<1>(10); // ERROR
f<2>(5.5); // ERROR
f<3>(7); // ERROR
return 0;
---
The error message on all 3 lines is:
error C2668: 'f' : ambiguous call to overloaded function
The compiler's "explanation" for the first one: (for the other two it is
much
the same except the type)
could be 'boost::enable_if_c f<1,int>(int)'
with
[
B=false,
T=void
]
or 'boost::enable_if_c f<1,int>(int)'
with
[
B=true,
T=void
]
while trying to match the argument list '(int)'
--------------------------
Version B) : type template parameter
--------------------------
#include <iostream>
#include
#include
using namespace std;
using namespace boost;
struct A1 {};
struct A2 {};
template
enable_if< is_same >
g(T t)
{
cout << 1 << endl;
}
template
enable_if< is_same >
g(T t)
{
cout << 2 << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
g<A1>(10); // ERROR
g<A2>(5.5); // ERROR
g<int>(7); // ERROR
return 0;
}
----
Error messages:
error C2668: 'g' : ambiguous call to overloaded function
(for all three of them)
"Explanation" sample:
could be 'boost::enable_if g(int)'
with
[
Cond=boost::is_same,
T=void
]
or 'boost::enable_if g(int)'
with
[
Cond=boost::is_same,
T=void
]
while trying to match the argument list '(int)'
-----------------
enable_if should actually prevent the compiler from trying to compile the
'false' version, shouldn't it?
Thx,
Agoston