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

标签 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;
}

为什么?谢谢!

错误信息是:

error C2039: 'status_': is not a member of 'Foo'

error C2065: 'status_': undeclared identifier

error C2131: expression did not evaluate to a constant

最佳答案

定义 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;
}

关于c++ - 模板代码在 G++ 下工作正常,但在 VC++ 下出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35682420/

相关文章:

c++ - C++中的函数地址文字在哪里?

c++ - 如何找到等于总和的子序列的最大子集的大小

c++ - 我如何指示 extconf.rb 使用额外的 g++ 优化标志,哪些是可取的?

c++ - 术语 `function declaration` 在 §7/9 (N4140) 中定义,但未定义为语法产生式。为什么?

java - 为什么我不能使用实例变量访问接口(interface)的静态方法

c++ - 使用点后模板函数的特化会破坏编译

c++ - 函数指针的取消引用是如何发生的?

c++ - 返回是原子的,我应该在 getter 中使用临时的线程安全吗?

c++ - 了解什么是原子约束

c++ - C++ lambda 真的会复制通过复制捕获的参数吗?