|
Boost Users : |
From: Agoston Bejo (gusz1_at_[hidden])
Date: 2005-01-02 15:17:49
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 <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
using namespace std;
using namespace boost;
template<unsigned int N, typename T>
enable_if_c<N==1>
f(T t)
{
cout << 1 << endl;
}
template<unsigned int N, typename T>
enable_if_c<N==2>
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<B,T> f<1,int>(int)' with [ B=false, T=void ] or 'boost::enable_if_c<B,T> 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 <boost/utility/enable_if.hpp> #include <boost/type_traits.hpp> using namespace std; using namespace boost; struct A1 {}; struct A2 {}; template<typename U, typename T> enable_if< is_same<U, A1> > g(T t) { cout << 1 << endl; } template<typename U, typename T> enable_if< is_same<U, A2> > 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<Cond,T> g<A1,int>(int)' with [ Cond=boost::is_same<A1,A2>, T=void ] or 'boost::enable_if<Cond,T> g<A1,int>(int)' with [ Cond=boost::is_same<A1,A1>, 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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net