std::函数,返回类型为void,参数为模板化
std::function with void return type and templated parameter
我有一个
template<class R>
class MyClass
{
public:
typedef std::function<void (const R)> ...;
};
在我尝试使用MyClass<void>。
在这种情况下,编译器将typedef扩展到
typedef std::function<void (void)> ...;
并且不想合作。
如果void被用作R参数,我希望typedef的行为像:
typedef std::function<void ()> ...;
由于这个类很大,我更喜欢type_traits和enable_if之类的东西,而不是为void创建专门化。
如注释中所述,您可以使用一个助手类:
template<class R>
struct MyClassHelper
{
using function_type = std::function<void (const R)>;
};
template <>
struct MyClassHelper<void>
{
using function_type = std::function<void ()>;
};
然后,在MyClass
中
template<class R>
class MyClass
{
public:
using function_type = typename MyClassHelper<R>::function_type;
};
相关文章:
- 在派生函数中指定void*参数
- void 函数可以传递值参数吗?
- 如何在C++中使用 Void 而不会收到有关参数的错误
- std::function<void()> 接受参数
- C++ 类型的参数与 void (__cdecl*)(void) 类型的参数不兼容,当调用 std::atexit()
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 将可变参数模板类型转换为 void,预期')'之前
- "(void) cast"与功能有什么区别 "__attributes__"来沉默未使用的参数警告?
- 如何成功地将函数对象(或lambda)传递给trackbar回调的第二个参数(void*)
- 带有void类型和参数的C++11模板专用化
- 如何将变量的值安全地传递给C++方法,该方法只将void*作为参数
- void 函数中的指针参数返回不一致的值
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- C++ void 作为参数,如 PHP
- sqlite3 更改函数回调参数 void* not用于 int* C++
- C PIMPL成语,类模板和void模板参数
- 无法将参数 1 从"int (__thiscall A::* *)(void)"转换为"int (__cdecl *)(void)"
- 如何警告 C 中 void 指针上的参数不兼容的类型
- 非类型模板参数可以是 "void*" 类型吗?
- 有人能解释参数void(*pfn)(void*)吗