在派生的模板中继承具有类型别名的构造函数 class
Inheriting constructor with a type alias in a template derived class
请看下面代码:
struct base {};
template <class T>
struct derived : T {
using base_type = T;
using base_type::T;
};
int main()
{
derived<base> x;
}
GCC 接受此代码,但 Clang 和 MSVC 拒绝它。谁是对的,为什么?
using base_type::T;
是一个 declaration, and the using
before it is an alias. This is a bit of an edge case in the standard, since the real question boils down to where does the T
get expanded. The C++ committee was referenced here 表示他们不打算让该语法有效,因此 LLVM 明确删除了它。看起来标准中没有任何内容阻止它,因此 gcc 在允许转换方面并没有错。谁是 'correct' 由你决定。
请看下面代码:
struct base {};
template <class T>
struct derived : T {
using base_type = T;
using base_type::T;
};
int main()
{
derived<base> x;
}
GCC 接受此代码,但 Clang 和 MSVC 拒绝它。谁是对的,为什么?
using base_type::T;
是一个 declaration, and the using
before it is an alias. This is a bit of an edge case in the standard, since the real question boils down to where does the T
get expanded. The C++ committee was referenced here 表示他们不打算让该语法有效,因此 LLVM 明确删除了它。看起来标准中没有任何内容阻止它,因此 gcc 在允许转换方面并没有错。谁是 'correct' 由你决定。