VS2005中模板参数阴影如何工作? [英] How does template argument shadowing work in VS2005?

查看:126
本文介绍了VS2005中模板参数阴影如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In GCC this code won't compile, because T gets shadowed, however in VS2005 it compiles with no warnings, so what are the assumptions VS compiler is making?

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


推荐答案

在搜索3个月后找到正确答案:)它位于标准的 14.6.1 / 4 中:

Found the right answer after 3 months of searching :) It's in 14.6.1/4 of the Standard:


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

A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.

示例:

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


它编译没有错误,甚至警告,它不符合。我不知道什么可以驱使它,允许它没有呻吟。你可以尝试高警告水平。

If the Microsoft compiler let it compile without errors or even warnings, it is not conforming. I don't know what could drive it to allow it without moaning. You could try to high warning levels.

这篇关于VS2005中模板参数阴影如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆