0

以下在 g++ 中编译,但不在 Visual C++ (Visual Studio Express 2012) 中。为什么?

如果我要使用(例如)foo以外的类型进行调用,那么我希望这会中断,因为在这种情况下是没有意义的。但如果我打算只用类型调用,那么它应该可以工作。事实上,即使我根本不调用,VC++ 中的编译也会失败!intdoubleB<double>::bbb:aaafoointfoo

struct A {
    typedef int aaa;
};

template <typename T>
struct B {
    typedef void bbb;
};

template <>
struct B<int> {
    typedef A bbb;
};

template <typename T>
typename B<T>::bbb::aaa // line 16
foo(const T &arg) {     // line 17
    return typename B<T>::bbb::aaa();
}

int main() {
    // Compilation fails with or without the following two lines.
    //int x = 0;
    //foo(x);
}

这是我收到的错误消息:

gcc_vs_vs.cc(16) : error C2510: 'bbb' : left of '::' must be a class/struct/union
gcc_vs_vs.cc(17) : error C2146: syntax error : missing ';' before identifier 'foo'
gcc_vs_vs.cc(17) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
gcc_vs_vs.cc(17) : error C2143: syntax error : missing ',' before '&'
4

0 回答 0