简体   繁体   English

在嵌套名称说明符中使用 decltype

[英]Using decltype in a nested-name-specifier

Consider the following demonstrative program.考虑以下演示程序。

#include <iostream>

namespace N
{
    struct A
    {
        static int n;
    };
    
    A A;
}

int N::A::n = 10;

int main() 
{
    std::cout << N::A::n << '\n';
    std::cout << N::decltype( N::A )::n << '\n';
    
    return 0;
}

The program compiles successfully using gcc 8.3 at for example www.ideone.com .该程序使用 gcc 8.3 成功编译,例如www.ideone.com

However if to run this program using MS VS 2019 then the compiler issues an error relative to the record decltype( N::A ) in the nested-name-specifier.但是,如果使用 MS VS 2019 运行此程序,则编译器会发出与嵌套名称说明符中的记录decltype( N::A )相关的错误。 If to remove preceding name N:: then the program compiles successfully.如果去掉前面的名字N::则程序编译成功。

Is it a bug of the MS VS compiler or is the nested-name-specifier written incorrectly?是 MS VS 编译器的错误还是嵌套名称说明符写错了?

A decltype-specifier can never appear except at the beginning of a nested-name-specifier . decltype-specifier永远不会出现,除非在nested-name-specifier的开头。 After all, it designates a specific type, and no name lookup is necessary afterwards to interpret it.毕竟,它指定了一个特定的类型,之后不需要名称查找来解释它。 GCC is wrong to accept the code: by experimentation, it seems to just ignore any preceding components after checking that they exist. GCC 接受代码是错误的:通过实验,在检查它们是否存在之后似乎只是忽略了任何前面的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM