类模板中成员变量的顺序
the order of member variable in class template
我已经定义了类模板和一个函数
template <typename F> class Base {
public:
Base(F ff): f(ff) {}
template <typename... Ps> auto operator() (Ps... ps) const -> decltype(f(ps...)) { return f(ps...); }
private:
// f is a pointer to function
F* f;
};
int f(int i, int j)
{
return i + j;
}
int main()
{
using f_type = remove_reference<decltype(f)>::type;
Base<f_type> b{f};
b(2, 5); // [Error] no match for call to '(Base<int(int, int)>) (int, int)'
}
报告了标记的错误。但是当我更改成员变量的顺序时 class Base
,喜欢:
template <typename F> class Base {
private:
// f is a pointer to function
F* f;
public:
Base(F ff): f(ff) {}
template <typename... Ps> auto operator() (Ps... ps) const -> decltype(f(ps...)) { return f(ps...); }
};
它可以编译。
这两个不同结果的原因是什么?谢谢您的宝贵时间!
声明以C 的顺序在源中看到。事物不同的值得注意的例外是成员函数的 bodies :当在类声明中定义成员函数时,定义(而不是其声明)的行为,好像该函数在类之后立即定义定义。
由于定义位置的规则 do 不适用于声明在成员函数声明中使用的名称需要声明在此刻。更改会员的位置提供必要的声明。
相关文章:
- 在循环中按顺序遍历成员变量
- C++编译器是否优化了顺序静态变量读取?
- 按字母顺序对结构内数组变量中的名称进行排序
- 字符串变量顺序
- 变量按什么顺序相乘
- 视觉C++:在 DLL 加载期间,全局变量初始化顺序是否具有确定性?
- 线程局部变量的初始化顺序
- 有了memory_order_relaxed,原子变量的总修改顺序如何在典型体系结构上得到保证
- 类静态变量初始化顺序
- 具有静态存储持续时间的常量初始化变量的初始化顺序
- 使用 while 循环显示顺序变量的值
- 同一函数中的静态函数变量初始化顺序
- 当变量的顺序发生变化时C++双倍乘法会产生不同的结果
- C 11指定静态/全局变量的构造顺序
- 单个Cout中单个变量的多个更新的顺序
- 为什么在定义静态成员变量时不遵循定义顺序
- 静态全局变量初始化顺序
- 类模板中成员变量的顺序
- 我可以在C 中更改静态变量初始化顺序吗?
- 为什么分段错误是由类变量顺序引起的