模板化代码在G ++下工作正常,但在VC++下出错

bla*_*all 5 c++ language-lawyer

以下是代码,可以正常工作g++,但在下面给出错误VC++ 2014:

template <class A>
struct Expression 
{
public:
    static const int status = A::status_;
}; 

struct Foo : public Expression<Foo>
{
    static const int  status_ = 0;
};

int main(void) {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么?谢谢!

错误消息是:

错误C2039:'status_':不是'Foo'的成员

错误C2065:'status_':未声明的标识符

错误C2131:表达式未计算为常量

The*_*ign 2

定义status它就会起作用。见下文。至于标准,我不知道哪个编译器是正确的。

template <class A>
struct Expression
{
public:
  static const int status;
};

struct Foo : public Expression<Foo>
{
  static const int  status_ = 0;
};

template< typename A >
const int Expression<A>::status = A::status_;

int main( void ) {
  return 0;
}
Run Code Online (Sandbox Code Playgroud)