模板继承中的 C++ 成员变量初始化 class
C++ Member variable initialization in templated inherited class
我想弄清楚这段代码有什么问题。基本上 type2
继承自 type1<T>, type1<T2>
,我想从基础 类.
之一初始化 value
成员
#include <utility>
template <typename T>
struct type1 {
using base_type = T;
template <typename... Args> type1(Args&&... args) : value(std::forward<Args>(args)...) {}
T value;
};
template <typename... Ts>
struct type2 : public Ts... {
template <typename T>
type2(T&& arg) : T::value(std::move(arg.value)) {}
};
int main()
{
type2<type1<int>, type1<double>> x(type1<int>(10));
return 0;
}
但是我从 clang 得到以下错误:
Error(s):
source_file.cpp:15:25: error: typename specifier refers to non-type member 'value' in 'type1<int>'
type2(T&& arg) : T::value(std::move(arg.value)) {}
^~~~~
source_file.cpp:20:38: note: in instantiation of function template specialization 'type2<type1<int>, type1<double> >::type2<type1<int> >' requested here
type2<type1<int>, type1<double>> x(type1<int>(10));
^
source_file.cpp:9:7: note: referenced member 'value' is declared here
T value;
^
1 error generated.
为什么 clang 说 typename specifier refers to non-type member 'value' in 'type1<int>'
? Gcc 想将(也可能是 clang)value
视为一种类型:
Error(s):
source_file.cpp: In instantiation of ‘type2<Ts>::type2(T&&) [with T = type1<int>; Ts = {type1<int>, type1<double>}]’:
source_file.cpp:20:54: required from here
source_file.cpp:15:51: error: no type named ‘value’ in ‘struct type1<int>’
type2(T&& arg) : T::value(std::move(arg.value)) {}
^
您不能在构造函数初始化列表中初始化基 class 的成员。
在标准语中,type2(T&& arg) : T::value(std::move(arg.value)) {}
中的 T::value(std::move(arg.value))
称为 mem-initializer,而 T::value
称为 内存初始化程序 ID。根据[class.base.init]p2、
Unless the mem-initializer-id names the constructor's class, a non-static data member of the constructor's class, or a direct or virtual base of that class, the mem-initializer is ill-formed.
可以调用基class的构造函数,让它初始化成员。在这种特定情况下,您只需将 T::value(std::move(arg.value))
更改为 T(std::move(arg.value))
。 Demo.
我想弄清楚这段代码有什么问题。基本上 type2
继承自 type1<T>, type1<T2>
,我想从基础 类.
value
成员
#include <utility>
template <typename T>
struct type1 {
using base_type = T;
template <typename... Args> type1(Args&&... args) : value(std::forward<Args>(args)...) {}
T value;
};
template <typename... Ts>
struct type2 : public Ts... {
template <typename T>
type2(T&& arg) : T::value(std::move(arg.value)) {}
};
int main()
{
type2<type1<int>, type1<double>> x(type1<int>(10));
return 0;
}
但是我从 clang 得到以下错误:
Error(s):
source_file.cpp:15:25: error: typename specifier refers to non-type member 'value' in 'type1<int>'
type2(T&& arg) : T::value(std::move(arg.value)) {}
^~~~~
source_file.cpp:20:38: note: in instantiation of function template specialization 'type2<type1<int>, type1<double> >::type2<type1<int> >' requested here
type2<type1<int>, type1<double>> x(type1<int>(10));
^
source_file.cpp:9:7: note: referenced member 'value' is declared here
T value;
^
1 error generated.
为什么 clang 说 typename specifier refers to non-type member 'value' in 'type1<int>'
? Gcc 想将(也可能是 clang)value
视为一种类型:
Error(s):
source_file.cpp: In instantiation of ‘type2<Ts>::type2(T&&) [with T = type1<int>; Ts = {type1<int>, type1<double>}]’:
source_file.cpp:20:54: required from here
source_file.cpp:15:51: error: no type named ‘value’ in ‘struct type1<int>’
type2(T&& arg) : T::value(std::move(arg.value)) {}
^
您不能在构造函数初始化列表中初始化基 class 的成员。
在标准语中,type2(T&& arg) : T::value(std::move(arg.value)) {}
中的 T::value(std::move(arg.value))
称为 mem-initializer,而 T::value
称为 内存初始化程序 ID。根据[class.base.init]p2、
Unless the mem-initializer-id names the constructor's class, a non-static data member of the constructor's class, or a direct or virtual base of that class, the mem-initializer is ill-formed.
可以调用基class的构造函数,让它初始化成员。在这种特定情况下,您只需将 T::value(std::move(arg.value))
更改为 T(std::move(arg.value))
。 Demo.