签名模板的 C++ 模板专业化 class
C++ templates specialization for signature template class
我正在创建签名模板 class。我遇到了“void”return 类型的问题。这是我处理 void return 类型的解决方案,我为它创建了一个模板专业化 class。
template<typename TSignature>
class CAction;
template<typename TRetType, typename... Args>
class CAction<TRetType(Args...)> {
public:
TRetType func(Args... a)
{
cout << "non void" << endl;
myPrint(a...);
cout <<"\n\n";
TRetType nRet(0);
return nRet;
}
};
template<typename... Args>
class CAction<void(Args...)> {
public:
void func(Args... a)
{
cout << "void" << endl;
myPrint(a...);
cout << "\n\n";
}
};
下面是我如何初始化 class。
CAction< void(int a, int b, double c, std::string d)> on_action1;
on_action1.func(1, 2, 10.0, "a love b");
CAction< double(int a, int b, double c, std::string d)> on_action2;
on_action2.func(1, 2, 10.0, "a love b");
上面的代码工作正常。
我只是好奇,除了上述方法,还有更好的解决方案吗?例如:我可以创建一个模板专业化成员函数 (func) 来处理“void”return 类型吗?如果您知道更多详细信息,请告诉我代码示例,非常感谢。
如果您可以访问 C++17,则可以在每个分支中输入 if constexpr
和 return 适当的值:
TRetType func(Args... a) {
if constexpr(!std::is_void<TRetType>{}) {
cout << "non void" << endl;
TRetType nRet(0);
return nRet;
}
else {
cout << "void" << endl;
}
}
否则,您可以使用标签分派技术根据 return 类型进行重载。由于无法使用非模板函数进行专门化:
template<class T>
struct type {};
TRetType func(type<TRetType>, long, Args... a) {
cout << "non void" << endl;
TRetType nRet(0);
return nRet;
}
void func(type<void>, int, Args... a) {
cout << "void" << endl;
}
TRetType func(Args... a) {
return func(type<TRetType>{}, 0, a...);
}
我正在创建签名模板 class。我遇到了“void”return 类型的问题。这是我处理 void return 类型的解决方案,我为它创建了一个模板专业化 class。
template<typename TSignature>
class CAction;
template<typename TRetType, typename... Args>
class CAction<TRetType(Args...)> {
public:
TRetType func(Args... a)
{
cout << "non void" << endl;
myPrint(a...);
cout <<"\n\n";
TRetType nRet(0);
return nRet;
}
};
template<typename... Args>
class CAction<void(Args...)> {
public:
void func(Args... a)
{
cout << "void" << endl;
myPrint(a...);
cout << "\n\n";
}
};
下面是我如何初始化 class。
CAction< void(int a, int b, double c, std::string d)> on_action1;
on_action1.func(1, 2, 10.0, "a love b");
CAction< double(int a, int b, double c, std::string d)> on_action2;
on_action2.func(1, 2, 10.0, "a love b");
上面的代码工作正常。 我只是好奇,除了上述方法,还有更好的解决方案吗?例如:我可以创建一个模板专业化成员函数 (func) 来处理“void”return 类型吗?如果您知道更多详细信息,请告诉我代码示例,非常感谢。
如果您可以访问 C++17,则可以在每个分支中输入 if constexpr
和 return 适当的值:
TRetType func(Args... a) {
if constexpr(!std::is_void<TRetType>{}) {
cout << "non void" << endl;
TRetType nRet(0);
return nRet;
}
else {
cout << "void" << endl;
}
}
否则,您可以使用标签分派技术根据 return 类型进行重载。由于无法使用非模板函数进行专门化:
template<class T>
struct type {};
TRetType func(type<TRetType>, long, Args... a) {
cout << "non void" << endl;
TRetType nRet(0);
return nRet;
}
void func(type<void>, int, Args... a) {
cout << "void" << endl;
}
TRetType func(Args... a) {
return func(type<TRetType>{}, 0, a...);
}