C++11 - template std::enable_if and std::result_of
C++11 - template std::enable_if and std::result_of
我已经创建了这个专门用于void/非void方法的模板
template <typename ClassType,
typename MethodType, MethodType MethodName,
typename std::enable_if <std::is_same<void, std::result_of<decltype(MethodName)(ClassType)>::type>::value> ::type* = nullptr
>
static int function()
{
//void
//....
}
template <typename ClassType,
typename MethodType, MethodType MethodName,
typename std::enable_if <!std::is_same<void, std::result_of<decltype(MethodName)(ClassType)>::type>::value> ::type* = nullptr
>
static int function()
{
//non-void
//....
}
//And I want to call it like this
function<Foo, void (Foo::*)(void), &Foo::Print>();
function<Foo, int (Foo::*)(void), &Foo::Print2>();
(基于这个答案:c++模板参数作为函数调用名)
然而,这给了我一堆错误(MSVC 2015)。如果我在
里面运行这个template <typename ClassType,
typename MethodType, MethodType MethodName
>
static int print()
{
std::cout << "C: " << std::is_same<void, std::result_of<decltype(MethodName)(ClassType)>::type>::value << std::endl;
}
我得到了true
结果。
是否可以为MethodName
的void/non-void结果"创建"专门化函数?
在GCC下可以正常编译
#include <iostream>
#include <type_traits>
using namespace std;
template <typename ClassType, typename MethodType, MethodType MethodName>
static auto function()
-> std::enable_if_t<std::is_void<typename std::result_of<MethodType(ClassType)>::type>::value, int>
{
//void
//....
}
template <typename ClassType, typename MethodType, MethodType MethodName>
static auto function()
-> std::enable_if_t<!std::is_void<typename std::result_of<MethodType(ClassType)>::type>::value, int>
{
//non-void
//....
}
我不确定这是否是你正在寻找,但我移动enable_if返回类型使用箭头语法,这看起来只是更干净的我。另外,既然你已经有了MethodType类型,为什么还要在MethodName上使用decltype呢?同样,result_of在访问类型之前需要typename。
如上所述,虽然没有可能的使用,但我不确定这是否是您所追求的。
注意:std::enable_if_t
在c++ 14中是可用的,如果你不能使用typename std::enable_if<...>::type
来代替。
你正好错过了typename
。
我建议:
template <typename F, F f> struct function_helper;
template <typename C, typename ... Ts, void (C::*m)(Ts...)>
struct function_helper<void (C::*)(Ts...), m>
{
int operator()() const { /* void implementation*/ }
};
template <typename C, typename Ret, typename ... Ts, void (C::*m)(Ts...)>
struct function_helper<Ret (C::*)(Ts...), m>
{
int operator()() const { /* non-void implementation*/ }
};
template <typename F, F f>
static int function()
{
return function_helper<F, f>{}();
}
与使用function<void (Foo::*)(), &Foo::Print>();
function<int (Foo::*)(), &Foo::Print2>();
或
function<decltype(&Foo::Print), &Foo::Print>();
function<decltype(&Foo::Print2), &Foo::Print2>();
在c++ 17中,可以使用template <auto f> struct function_helper;
相关文章:
- Usages of std::move
- 具有变量Number of Arguments的std::函数的矢量
- constexpr begin of a std::array
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- Centos7 g++ "to_string is not in a member of std"
- 传递 std::vector of std::shared_ptr,而不是更新对象
- 初始化 std::数组 of Eigen::Map
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- PyBind11:返回对 std::vector of std::unique_ptr 的常量引用
- 是否可以使用 std::variant of std::variants
- What is the std::chrono::time_point equivalent of std::numer
- python equivalent of std::chrono::steady_clock::now();
- Strange behavior of std::vector<{QString,int*}>
- 如何在 std::map 中从 std::vector of std::p air 中获取输入?
- 使用 operator[] 访问私有 std::map of unique_ptr
- "terminate called after throwing an instance of std::invalid_argument' what(): stoi ?"
- 在 std::unordered_map 中插入新的键/值对会导致"out of range"异常
- 使类在流上模板化处理std::cout和std::of流
- 使用初始化器列表初始化类中的std::of流
- std::of流默认情况下是截断还是附加