我正在尝试为Foo<T>::bar()
以下特定类型重载该方法T
——但没有成功。我很感激指针和解决方法。
#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;
}
在ideone上编译它会产生以下编译器错误:
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()