C++ 如何使用enable_if定义不同的成员函数?
我的代码是:C++ 如何使用enable_if定义不同的成员函数?,c++,templates,g++,traits,C++,Templates,G++,Traits,我的代码是: template<int s> struct C{ typename std::enable_if<s == 1, int>::type fun(){std::cout<<"fun1";} typename std::enable_if<s == 2, int>::type fun(){std::cout<<"fun2";} }; int main() { C<1> c
template<int s>
struct C{
typename std::enable_if<s == 1, int>::type
fun(){std::cout<<"fun1";}
typename std::enable_if<s == 2, int>::type
fun(){std::cout<<"fun2";}
};
int main() {
C<1> c;
c.fun();
}
我的编译器是g++4.1.2,如果在函数之前使用
模板
,它会警告这是C++11的特性。我想知道如何在不使用C++11的情况下解决这个问题?如果您能够实现enable\u If
特性,您可以按如下方式重新排列代码(最简单、完整的示例)以使其正常工作:
#include<type_traits>
#include<iostream>
struct C {
template<int s>
typename std::enable_if<s == 1, int>::type
fun() { std::cout<<"fun1"; }
template<int s>
typename std::enable_if<s == 2, int>::type
fun() { std::cout<<"fun2"; }
};
int main() {
C c;
c.fun<1>();
}
如果您希望类被实例化为C
,那么这次就不能简单地使用SFINAE了。请注意,
s
在您决定使用模板类的编译时是已知的,因此您可以随时使用它。它遵循一种可能的解决方案:
#include<type_traits>
#include<iostream>
template<int s>
struct C {
void fun() {
if(s == 1) {
std::cout<<"fun1";
} else if(s == 2) {
std::cout<<"fun2";
}
}
};
int main() {
C<1> c;
c.fun();
}
#包括
#包括
模板
结构C{
虚无乐趣(){
如果(s==1){
std::cout如果您无法实现注释中提到的std::enable_If
,我有一个如下所示的替代方案。解决方案依赖于编译器优化switch语句,但仍将使用非优化编译器
#include <iostream>
template <int s>
struct C {
int fun() {
switch (s) {
case 1:
fun1();
break;
case 2:
fun2();
break;
default:
assert(0);
}
}
inline int fun1() { std::cout << "fun1"; }
inline int fun2() { std::cout << "fun2"; }
};
即使我不建议这种方法,它也遵循另一种可能的解决方案。
出于好奇,我将其作为单独的答案添加,因为我发现另一个答案最合适。
无论如何,这一个可能是有趣的,因为它展示了如何使用sfinae和函数解析来解决这样的问题
#include<iostream>
#include<type_traits>
template<int s>
struct C {
template<int i>
typename std::enable_if<i == 1>::type
fun(){std::cout<<"fun1"<<std::endl;}
template<int i>
typename std::enable_if<i == 2>::type
fun(){std::cout<<"fun2"<<std::endl;}
void fun() {
fun<s>();
}
};
int main() {
C<1> c1;
c1.fun();
C<2> c2;
c2.fun();
}
#包括
#包括
模板
结构C{
模板
typename std::enable_if::type
乐趣{std::cout4.1.2?你为什么要使用一个超过9年的编译器?4.1.2不支持c++11功能请看这里@JerryCoffin许多以前的代码都是基于编译器的。我们不能更改它。@brufalobill还有其他方法吗?@maple你有权访问以前的源代码吗?谢谢你的回答。很抱歉忘记了放置主函数。我编辑了cod,我想这样调用它。你有什么想法吗?
#include<type_traits>
#include<iostream>
template<int s>
struct C;
template<>
struct C<1> {
void fun() { std::cout<<"fun1"; }
};
template<>
struct C<2> {
void fun() { std::cout<<"fun2"; }
};
int main() {
C<1> c;
c.fun();
}
#include <iostream>
template <int s>
struct C {
int fun() {
switch (s) {
case 1:
fun1();
break;
case 2:
fun2();
break;
default:
assert(0);
}
}
inline int fun1() { std::cout << "fun1"; }
inline int fun2() { std::cout << "fun2"; }
};
// Primary template.
/// Define a member typedef @c type only if a boolean constant is true.
template<bool, typename _Tp = void>
struct enable_if
{ };
// Partial specialization for true.
template<typename _Tp>
struct enable_if<true, _Tp>
{ typedef _Tp type; };
#include<iostream>
#include<type_traits>
template<int s>
struct C {
template<int i>
typename std::enable_if<i == 1>::type
fun(){std::cout<<"fun1"<<std::endl;}
template<int i>
typename std::enable_if<i == 2>::type
fun(){std::cout<<"fun2"<<std::endl;}
void fun() {
fun<s>();
}
};
int main() {
C<1> c1;
c1.fun();
C<2> c2;
c2.fun();
}