【问题标题】:How does template argument shadowing work in VS2005?模板参数阴影在 VS2005 中是如何工作的?
【发布时间】:2009-01-28 04:44:45
【问题描述】:

在 GCC 中这段代码不会编译,因为 T 被遮蔽了,但是在 VS2005 中它编译时没有任何警告,那么 VS 编译器所做的假设是什么?

template<typename T>
class Foo
{
    template<typename T>
    void Bar(const T& bar)
    {
      ...
    }
};

【问题讨论】:

  • 你在 gcc 下得到的确切错误信息是什么?
  • 类似“T is shadowing class scope T”

标签: c++ templates gcc visual-studio-2005


【解决方案1】:

经过 3 个月的搜索,找到了正确的答案 :) 在标准的 14.6.1/4 中:

不应在其范围内重新声明模板参数(包括嵌套范围)。模板参数的名称不应与模板名称相同。

例子:

template<class T, int i> class Y {
    int T;
    // error: template-parameter redeclared
    void f() {
        char T;
        // error: template-parameter redeclared
    }
};

template<class X> class X; // error: template-parameter redeclared

如果 Microsoft 编译器让它编译时没有错误甚至警告,则它不符合要求。我不知道是什么驱使它允许它不呻吟。您可以尝试高警告级别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2018-05-02
    • 2020-01-09
    • 2018-02-03
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多