2015-07-10 94 views
1

在VC++ 2013(和2015年RC)的名称模板的说法,我觉得这会导致编译错误:一类的另一个命名空间

namespace namespace1 
{ 
    template<typename T> 
    class Bar 
    {}; 
} 

namespace namespace2 
{ 
    template <unsigned Bar> 
    struct Foo 
    { 
     static const int value = (Bar < 1) ? 1 : 2; 
    }; 
} 

错误:

error C2059: syntax error : ')' 
: see reference to class template instantiation 'namespace2::Foo<Bar>' being compiled 
error C2143: syntax error : missing ')' before ';' 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
error C2143: syntax error : missing ';' before ')' 
error C2059: syntax error : ')' 
error C2238: unexpected token(s) preceding ';' 
fatal error C1201: unable to continue after syntax error in class template definition 

如果我交换命名空间的顺序,我不会收到错误。

为什么编译器将Bar当作一个类型处理,当它不符合命名空间的限定时?

另外,如果我改变的值初始化到:

static const int value = (Bar > 1) ? 1 : 2; 

我没有得到一个错误要么。

遇到这样在编译谷歌协议缓冲,在这个结构的定义:

// Compile-time equivalent of VarintSize32(). 
template <unsigned Value> 
struct StaticVarintSize32 { 
    static const int value = 
     (Value < (1 << 7)) 
      ? 1 
      : (Value < (1 << 14)) 
       ? 2 
       : (Value < (1 << 21)) 
        ? 3 
        : (Value < (1 << 28)) 
         ? 4 
         : 5; 
}; 

不会因所谓的价值模板类存在于我们自己的代码库命名空间编译。我现在通过确保首先包含相关的Procotol缓冲区头文件来解决它,但似乎这是一个编译器错误?

当我无法真正改变协议缓冲区代码或Value类时,是否有其他解决方法?

+0

看起来像一个bug给我。 'Bar'类模板在使用'Bar'的地方不可见。作为一种解决方法,您可以在'Bar'周围添加一对额外的parens,如'static const int value =((Bar)<1)? 1:2;' –

+0

如上所述,这是在协议缓冲区代码,虽然我可以改变它,但我不认为这是正确的修复。 – Chris

回答

0

确认为Microsoft的错误,将在2015 RTM中修复。

相关问题