【问题标题】:Inheriting constructor with a type alias in a template derived class [duplicate]在模板派生类中继承具有类型别名的构造函数[重复]
【发布时间】:2019-01-09 10:54:34
【问题描述】:

请看以下代码:

struct base {};

template <class T>
struct derived : T {
  using base_type = T;
  using base_type::T;
};

int main()
{
  derived<base> x;
}

GCC 接受此代码,但 Clang 和 MSVC 拒绝它。谁是对的,为什么?

【问题讨论】:

  • 我不知道。但如果这个 bug 鼓励你写得更清楚using base_type::base_type;using T::T;,我赞成。
  • @alfC 可笑的是,我确实写了(并且总是写)using base_type::base_type。以上代码是nvidia CUDA编译器(NVCC)生成的翻译。我不知道为什么 NVCC 将 base_type::base_type 翻译成 base_type::T,但这就是我遇到这个问题的方式。
  • 很高兴知道。也许有一种方法可以强制 nvcc 不更改代码的某些部分。

标签: c++ c++11 language-lawyer inheriting-constructors


【解决方案1】:

using base_type::T;declaration,而 using 之前的 alias。这是标准中的一个边缘案例,因为真正的问题归结为T 在哪里得到扩展。 C++ 委员会被引用 here 表示他们不打算让该语法有效,因此 LLVM 明确删除了它。看起来标准中没有任何东西阻止它,所以 gcc 在允许转换方面并没有错。谁是“正确的”取决于你。

【讨论】:

    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 2021-04-20
    • 2015-11-03
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多