Rau*_*nso 7 c++ templates template-meta-programming c++11
我编写了一个元函数来检索成员函数的第一个参数的类型,当然它接收一个或多个参数.我写的代码如下:
template <typename...> struct parameter;
template < typename O, typename A, typename R, typename... Args>
struct parameter <R (O::*)(A, Args...) > {
using first_param = A;
};
Run Code Online (Sandbox Code Playgroud)
我使用这个元函数如下:
using mem_fn = void(mem_type::*)(std::vector<int>);
using f_pm = parameter<mem_fn>::first_param;
Run Code Online (Sandbox Code Playgroud)
它编译和工作.但是当我上课时:
struct mem_type{
void update(std::vector<int>) {
}
};
Run Code Online (Sandbox Code Playgroud)
并使用我的元函数如下:
using mem_fn = decltype(mem_type::update);
using f_pm = parameter<mem_fn>::first_param;
Run Code Online (Sandbox Code Playgroud)
代码不编译,Visual Studio 2013给出:错误C2027:使用未定义类型parameter<mem_fn>.
有谁知道这个错误的原因?
首先,命名非静态成员函数(在本例中)的id 表达式mem_type::update不能用作未计算的操作数(例如 的操作数decltype)。\xc2\xa75.1.1 [expr.prim.general]/p13 (脚注省略):
\n\n\n只能使用表示类的非静态数据成员或非静态成员函数的id 表达式:
\n\n\n
\n- 作为类成员访问 (5.2.5) 的一部分,其中对象表达式引用 member\xe2\x80\x99s 类或从该类派生的类,或者
\n- 形成一个指向成员的指针(5.3.1),或者
\n- 如果该id 表达式表示非静态数据成员并且它出现在未计算的操作数中。
\n
\xc2\xa77.1.6.2 [dcl.type.simple]/p4:
\n\n\n\n\n说明符的操作数
\ndecltype是未计算的操作数\n(第 5 条)。
即使update是常规函数,decltype也会产生函数类型而不是函数指针类型,并且您的专业化与指向成员函数的指针类型相匹配。
&您需要使用- 即来创建一个指向成员函数的指针decltype(&mem_type::update)。
| 归档时间: |
|
| 查看次数: |
497 次 |
| 最近记录: |