我在写一个与std::function
(或者至少这些类在很多方面类似)共享几个不同特性的类。大家都知道std::function
通过指定模板参数(例如std::function<void (std::string&)>
)来实例化,这对我的班级来说是一样的。我有一个例外,但如果返回值是无效的(std::function<"return value" ("parameters">
),我想专门在我的课程中使用单个函数函数。我需要在编译时完成这个工作,而且我不能像它应该那样工作。下面是解释一些测试代码:编译时的类型特化
#include <iostream>
#include <type_traits>
template <typename T> class Test { };
template <typename Ret, typename... Args>
class Test<Ret (Args...)>
{
public:
Ret operator()(Args...)
{
if(std::is_void<Ret>::value)
{
// Do something...
}
else /* Not a void function */
{
Ret returnVal;
return returnVal;
}
}
};
int main(int argc, char * argv[])
{
Test<void (char)> test;
test('k');
}
正如你可以清楚地看到,如果编译器不会在上面的测试中去掉了“其他”的分支,我的代码将尝试创建一个空值(即void returnVal;
) 。问题是,所以我结束了一个编译器错误编译器不会删除分支:
./test.cpp: In instantiation of ‘Ret Test::operator()(Args ...) [with Ret = void; Args = {char}]’: ./test.cpp:27:10: required from here ./test.cpp:18:8: error: variable or field ‘returnVal’ declared void ./test.cpp:19:11: error: return-statement with a value, in function returning 'void' [-fpermissive]
人们通常会使用std::enable_if
与std::is_void
相结合,问题是,我不希望专注于功能模板,但在类模板。
template <typename Ret, typename... Args>
class Test<Ret (Args...)>
{
public:
typename std::enable_if<!std::is_void<Ret>::value, Ret>::type
Ret operator()(Args...)
{
Ret returnVal;
return returnVal;
}
typename std::enable_if<std::is_void<Ret>::value, Ret>::type
Ret operator()(Args...)
{
// It's a void function
// ...
}
};
如果我使用上面的代码,而不是我最终甚至更多的错误,并没有解决
./test.cpp:11:2: error: expected ‘;’ at end of member declaration
./test.cpp:11:2: error: declaration of ‘typename std::enable_if<(! std::is_void<_Tp>::value), Ret>::type Test<Ret(Args ...)>::Ret’
./test.cpp:6:11: error: shadows template parm ‘class Ret’
./test.cpp:11:24: error: ISO C++ forbids declaration of ‘operator()’ with no type [-fpermissive]
./test.cpp:18:2: error: expected ‘;’ at end of member declaration
./test.cpp:18:2: error: declaration of ‘typename std::enable_if<std::is_void<_Tp>::value, Ret>::type Test<Ret(Args ...)>::Ret’
./test.cpp:6:11: error: shadows template parm ‘class Ret’
./test.cpp:18:24: error: ISO C++ forbids declaration of ‘operator()’ with no type [-fpermissive]
./test.cpp:18:6: error: ‘int Test<Ret(Args ...)>::operator()(Args ...)’ cannot be overloaded
./test.cpp:11:6: error: with ‘int Test<Ret(Args ...)>::operator()(Args ...)’
./test.cpp: In member function ‘int Test<Ret(Args ...)>::operator()(Args ...)’:
./test.cpp:22:2: warning: no return statement in function returning non-void [-Wreturn-type]
./test.cpp: In instantiation of ‘int Test<Ret(Args ...)>::operator()(Args ...) [with Ret = void; Args = {char}]’:
./test.cpp:28:10: required from here
./test.cpp:13:7: error: variable or field ‘returnVal’ declared void
./test.cpp: In member function ‘int Test<Ret(Args ...)>::operator()(Args ...) [with Ret = void; Args = {char}]’:
./test.cpp:15:2: warning: control reaches end of non-void function [-Wreturn-type]
对不起,如果我只是普通的愚蠢,答案是显而易见的。我对模板相当陌生,在任何其他线索/问题中找不到合适的答案。
有一个建议,包括一个“静态如果”,可以做你想要在你的第一个例子。 – mfontanini
@mfontanini:这两个提案中的任何一个实际上是否都会向前推进是另一回事。他们有缺陷,并且一些东西变得比他们看起来复杂得多,如果你开始使用带有静态模板的SFINAE-if –
“问题是编译器没有移除分支,所以最终出现编译器错误”=“ >不幸的是,编译器*不需要*,这(我承认)有点不安。 –