具有零或一个参数的函数上的 Sfinae
Sfinae on function with either zero or one parameter
请考虑以下两个声明:
template <class Function,
class = typename std::enable_if</*Function has zero argument*/>::type>
void apply(Function&& function);
template <class Function,
class... Dummy,
class = typename std::enable_if</*Function has one argument*/>::type,
class = typename std::enable_if<sizeof...(Dummy) == 0>::type>
void apply(Function&& function, Dummy...);
在第一种和第二种情况下,当函数有零个或一个参数(无论参数的类型如何)时,我应该在std::enable_if
中放入什么来约束重载?
以下特征可能会对您有所帮助:
#include <type_traits>
#include <functional>
template <typename T>
struct arity : public arity<decltype(&T::operator())> {};
template <typename C, typename Ret, typename... Args>
struct arity<Ret(C::*)(Args...) const> :
std::integral_constant<std::size_t, sizeof...(Args)>
{
};
// Do the same for other (11) combination of volatile, const, reference method.
// function pointer
template<class R, class... Args>
struct arity<R(*)(Args...)> : public arity<R(Args...)>
{};
template<class R, class... Args>
struct arity<R(Args...)> : std::integral_constant<std::size_t, sizeof...(Args)>
{
};
然后
template <class Function,
class = typename std::enable_if<arity<typename std::remove_reference<Function>::type>::value == 0>::type>
void apply(Function&& function);
相关文章:
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何制作一个将函数作为参数的类方法
- 修改函数中的指针(将另一个指针作为参数传递)
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 构造函数在退出函数时无法初始化一个参数
- 在C++中声明一个函数时,它需要有函数本身的参数吗
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 运算符重载:"operator+"必须采用零个或一个参数
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 如果需要转换,我可以在读取参数的同时将其移动到另一个参数吗?
- 如何在另一个函数中使用返回值作为参数?
- 如果模板参数是另一个模板的实例化,则键入特征测试
- 将参数一个接一个地传递,或通过将它们包裹在数组,结构或元组中
- 如何给一个参数一个由其他参数的函数确定的默认值
- C++如何给一个参数一个特定的枚举,它可以是
- 如果我在默认构造函数中放了一个参数,但给了这个参数一个默认值,它还是一个默认构造函数吗
- 为什么在c++中给typename模板参数一个默认值0 ?