2

Possible Duplicate:
std::enable_if to conditionally compile a member function

I'm trying to overload the method Foo<T>::bar() for specific types of T as follows -- without success. I'd appreciate pointers and workarounds.

#include <cstdlib>
#include <iostream>
#include <boost/type_traits.hpp>
#include <boost/utility/enable_if.hpp>

template<typename T>
struct Foo
{
    typename boost::enable_if_c<boost::is_same<char,T>::value >::type
    bar();

    typename boost::disable_if_c<boost::is_same<char,T>::value >::type
    bar();
};

template<typename T>
typename boost::disable_if_c<boost::is_same<char,T>::value >::type
Foo<T>::bar()
{
    std::cout << "I am generic ..." << std::endl;
}

template<typename T>
typename boost::enable_if_c<boost::is_same<char,T>::value >::type
Foo<T>::bar()
{
    std::cout << "I am specific ..." << std::endl;
}

int main()
{
    Foo<char> f;
    f.bar();

    return EXIT_SUCCESS;
}

Compiling this on ideone yields the following compiler errors:

prog.cpp:13: error: ‘typename boost::disable_if_c<boost::is_same::value, void>::type Foo<T>::bar()’ cannot be overloaded
prog.cpp:10: error: with ‘typename boost::enable_if_c<boost::is_same::value, void>::type Foo<T>::bar()’
prog.cpp:18: error: prototype for ‘typename boost::disable_if_c<boost::is_same::value, void>::type Foo<T>::bar()’ does not match any in class ‘Foo<T>’
prog.cpp:10: error: candidate is: typename boost::enable_if_c<boost::is_same::value, void>::type Foo<T>::bar()
Community
  • 1
  • 1
Olumide
  • 5,397
  • 10
  • 55
  • 104
  • 1
    +1: Did not know you could use Boost on ideone. – John Dibling Oct 10 '12 at 14:43
  • You're missing an include: `#include ` (among other issues). – Luc Touraille Oct 10 '12 at 14:45
  • @LucTouraille makes little difference, but I'll include it anyway. – Olumide Oct 10 '12 at 14:48
  • 1
    It *does* make a difference: now the compiler gives you an error message that actually describes the true problem with your code (that is, the fact that you are not using `enable_if` correctly, as explained in response to the question linked). – Luc Touraille Oct 10 '12 at 14:51
  • You are separating declaration from definition, which is not possible with templates. – Sdra Oct 10 '12 at 15:04
  • @Luc Touraille: you mean I can declare a template function in a .h and define it in a .cpp? – Sdra Oct 10 '12 at 15:59
  • @Sdra Under normal circumstances, template definitions have "got to seen" by the time they are instantiated. Therefore they've got to be in header files (*.h or *.hpp) – Olumide Oct 10 '12 at 16:03
  • @Sdra: There is no `.cpp` in the code posted by the OP, he simply defined the member functions below the class definition. Anyway, it is still possible to define templates into separate implementation files, but then you need to explicitly instantiate them for the set of types (or rather template arguments) that you wish to use them with (see the end of [this answer I posted a while ago](http://stackoverflow.com/a/495056/20984)). – Luc Touraille Oct 10 '12 at 19:41
  • Thanks for the clarification guys. I'm used to keep things simple and keep declaration and definition together in header, in respect to templates. I guess I never tried to separate them! – Sdra Oct 11 '12 at 08:36
  • @Sdra: Of course you can separate them, but usually you'll still have them in the same header. – Sebastian Mach Oct 12 '12 at 08:00

1 Answers1

0

You are using enable_if and disable_if with same parameters. Looks like you are enabling and disabling same template instance and compiler thinks that you are overloading it. Don't use is_same_char in both instances.

kkg
  • 69
  • 5
  • I thought the `enable_if` made one `Foo::bar` valid when `T=char` while the `disable_if` made `Foo::bar` valid when `T!=char`. – Olumide Oct 10 '12 at 16:29