|
Boost Users : |
From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-01-04 19:24:20
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
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