【发布时间】:2015-09-16 16:38:43
【问题描述】:
给定一个模板参数类T,它有一个单个构造函数(也没有复制或移动构造函数)和零个默认参数,有什么方法可以找到T(...) 的arity 吗?
到目前为止我的尝试
#include <iostream>
#include <string>
#include <vector>
template <typename F> struct function_arity;
template <typename R, typename... Args>
struct function_arity<R (Args...)>
: std::integral_constant<std::size_t, sizeof...(Args)> {};
template <typename R, typename... Args>
struct function_arity<R (*)(Args...)> : function_arity<R (Args...)> {};
template <typename R, typename... Args>
struct function_arity<R (&)(Args...)> : function_arity<R (Args...)> {};
template <typename R, typename C, typename... Args>
struct function_arity<R (C::*)(Args...) const> : function_arity<R (Args...)> {};
template <typename R, typename C, typename... Args>
struct function_arity<R (C::*)(Args...)> : function_arity<R (Args...)> {};
template <typename C>
struct function_arity : function_arity<decltype(&C::operator())> {};
struct no_copy { no_copy() = default; no_copy(const no_copy&) = delete; };
struct no_move { no_move() = default; no_move(no_move&&) = delete; };
struct A : no_copy, no_move { A(int, float) { std::cout << "A!\n"; }; };
struct B : no_copy, no_move { B(double) { std::cout << "B!\n"; }; };
struct C : no_copy, no_move { C() { std::cout << "C!\n"; }; };
int main()
{
std::cout << function_arity<&A::A>::value << "\n";
return 0;
}
【问题讨论】:
-
还有拷贝构造函数...
-
@Jarod 不,它和移动构造函数被标记为已删除。只有 一个 构造函数可用。
-
@BrianRodriguez:我的意思是删除的函数是重载的一部分Demo。
-
@Jarod 那么这不可能吗?
-
@BrianRodriguez 如果类型已知,这是可能的。
标签: c++ templates constructor arity