可能重复:
std::enable_if to conditionally compile a member function
我正在尝试为特定类型的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()
答案 0 :(得分:0)
您正在使用具有相同参数的enable_if和disable_if。看起来你正在启用和禁用相同的模板实例,编译器认为你正在重载它。不要在两个实例中使用is_same_char。