c++ - 当试图通过使用带有虚拟参数的模板别名来隐藏基类来混淆它们时,clang 和 gcc 之间的行为差​​异

标签 c++ templates language-lawyer name-lookup

考虑以下 C++ 程序:

class A
{
    protected:
        int x;
};

template<typename X>
using B = A;

template<typename T>
class C : public B<T>
{
    public:
        void f()
        {
            x = 0;
        }
};

int main()
{
}

当使用 -std=c++17 -pedantic-errors 作为编译选项使用 clang 和 gcc 进行编译时,它们的行为有所不同:Clang 编译时没有任何错误,但 gcc 给出有关不存在的编译错误能够查找标识符x

在这种情况下,C++ 标准是怎么说的?这两种行为是否都被允许,或者在这种情况下其中一个编译器是否存在错误?

编译器资源管理器链接:https://godbolt.org/z/EYvYrr

最佳答案

这看起来类似于 CWG1390 ,并且看起来 Clang 的行为与 CWG 关于如何处理此类别名模板的协议(protocol)一致(在模板定义时急切地替换):

1390. Dependency of alias template specializations

According to 17.7.2.1 [temp.dep.type] paragraph 8, a type is dependent (among other things) if it is

  • a simple-template-id in which either the template name is a template parameter or any of the template arguments is a dependent type or an expression that is type-dependent or value-dependent

This applies to alias template specializations, even if the resulting type does not depend on the template argument:

   struct B { typedef int type; };
   template<typename> using foo = B;
   template<typename T> void f() {
     foo<T>::type * x;  //error: typename required
   }

Is a change to the rules for cases like this warranted?

Notes from the October, 2012 meeting:

CWG agreed that no typename should be required in this case. In some ways, an alias template specialization is like the current instantiation and can be known at template definition time.

关于c++ - 当试图通过使用带有虚拟参数的模板别名来隐藏基类来混淆它们时,clang 和 gcc 之间的行为差​​异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63761866/

相关文章:

java - 如何在 IntelliJ IDEA 中创建具有默认文件名的模板?

c - "A conforming compiler may choose not to implement non-normalized floating point numbers"的意义是什么?

c++ - 我认为这是 C++11 标准中的一个(小)缺陷

c++ - 未初始化的枚举器默认值

c++ - 如何在运行时恢复函数指针的类型

templates - grails模板-脚手架 Controller

c# - 是否可以使用 C++ 解密在 Silverlight 中加密的数据?

c++ - BOOST_GEOMETRY_REGISTER_RING 的使用

c++ - C++平台游戏中的寻路

c++ - 在一维数组中填充二维数组