使用 enable_if 重载类模板的成员函数
Overloading member functions of class template with enable_if's
可能的重复项:
std::enable_if 有条件地编译成员函数
我正在尝试重载特定类型T
的方法Foo<T>::bar()
如下 - 但没有成功。我将不胜感激指示和解决方法。
#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()
您正在使用具有相同参数的enable_if和disable_if。看起来您正在启用和禁用相同的模板实例,并且编译器认为您正在重载它。不要在这两种情况下都使用 is_same_char。
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法