enable_if for function templates

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

Agoston Bejo wrote:
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
#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[])
_tmain, _TCHAR ... what are these funny symbols? ;-)
{ f<1>(10); // ERROR f<2>(5.5); // ERROR f<3>(7); // ERROR return 0; }
I'd say enable_if is not the right tool for this situation. Try: template<int N> struct f_impl { template<typename T> static void execute(T t) { cout << "f_impl::execute()" << "\n"; } }; template<int N, typename T> void f(T t) { f_impl<N>::execute(t); } template<> struct f_impl<1> { template<typename T> static void execute(T t) { cout << "1" << "\n"; } }; template<> struct f_impl<2> { template<typename T> static void execute(T t) { cout << "2" << "\n"; } }; int main() { f<1>(10); f<2>(5.5); f<3>(7); return 0; } Jonathan

"Agoston Bejo" <gusz1@freemail.hu> wrote in message news:cr9kth$s8u$1@sea.gmane.org...
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. template<unsigned int N, typename T> enable_if_c<N==1> f(T t) { cout << 1 << endl; }
FWIW enable_if signature is required as follows: template<unsigned int N, typename T> //enable_if_c<N==1> typename enable_if_c<N==1>::type f(T t) { cout << 1 << endl; } //...etc regards Andy Little
participants (3)
-
Agoston Bejo
-
Andy Little
-
Jonathan Turkanis