【发布时间】:2020-08-04 00:09:34
【问题描述】:
我想仅使用 C++11 构建一个模板帮助器对象,该对象可用于包装 C 函数。
我正在尝试将here 给出的答案从包装函数扩展到包装对象,以便它可以包含状态:
#include <iostream>
#include <functional>
int foo(int a, int b) { return a + b; }
template<typename Fn, Fn fn, typename... Args>
class AltFuncWrapper
{
public:
using result_type = typename std::result_of<Fn(Args...)>::type;
bool enabled{false};
result_type exec(Args... args)
{
if(enabled)
{
std::cout << "Run the real thing";
return fn(std::forward<Args>(args)...);
}
else
{
std::cout << "Return default value";
return result_type{};
}
}
};
int main()
{
AltFuncWrapper<decltype(&foo), &foo> wrapper{};
return 0;
}
但我得到以下编译器错误(CE link):
<source>: In instantiation of 'class TestDoubleWrapper<int (*)(const char*, unsigned int)throw (), chmod>':
<source>:68:51: required from here
<source>:30:67: error: no type named 'type' in 'class std::result_of<int (*())(const char*, unsigned int)throw ()>'
using result_type = typename std::result_of<Fn(Args...)>::type;
^
【问题讨论】:
标签: c++ c++11 variadic-templates