MSVC与GCC的差异(收集中)

1. 变长数组的支持.

void f(int len) { char buf[len]; } 

  GCC支持,MSVC不支持

 

 

2. 临时变量作为函数参数传递

 f(const QString& s);

g(QString& s);

 

f("abc");  //MSVC通过,GCC不通过

g("abc"); //MSVC和GCC都通过

 

 

3. 找不到静态常量的定义, undefined reference to static const variable

 

enum TypeId {B_TYPE=0, C_TYPE};

 

class B 

{

public:

static const TypeId B_ID=B_TYPE;

};

 

 

 

g((int)B::B_ID);  //都工作

g(+B::B_ID); //都工作

g(B::B_ID);  //MSVC工作,在GCC下不工作,

                   //GCC报告链接错误undefined reference to `B::B_ID'  找不到静态常量的定义

 

这个问题在http://gcc.gnu.org/ml/gcc-bugs/2005-03/msg02430.html 也有说,但GCC似乎不认为这是个bug

http://stackoverflow.com/questions/272900/c-undefined-reference-to-static-class-member 这个地方有个回答,说的比较清楚

 

The C++ standard requires a definition for your static const member if the definition is somehow needed.

The definition is required, for example if it's address is used. push_back takes its parameter by const reference, and so strictly the compiler needs the address of your member and you need to define it in the namespace.

When you explicitly cast the constant, you're creating a temporary and it's this temporary which is bound to the reference (under special rules in the standard).

This is a really interesting case, and I actually think it's worth raising an issue so that the std be changed to have the same behaviour for your constant member!

Although, in a weird kind of way this could be seen as a legitimate use of the unary '+' operator. Basically the result of the unary + is an rvalue and so the rules for binding of rvalues to const references apply and we don't use the address of our static const member:

v.push_back( +Foo::MEMBER );
 
     
Yes it's certainly weird that for an object x of type T, the expression "(T) x" can be used to bind a const ref while plain "x" can't. I love your observation about "unary +"! Who would have thought that poor little "unary +" actually had a use... :) –  j_random_hacker May 29 '09 at 10:38
 
Thinking about the general case... Is there any other type of object in C++ that has the property that it (1) can be used as an lvalue only if it has been defined but (2) can be converted to an rvalue without being defined? –  j_random_hacker May 29 '09 at 10:51
 
Good question, and at least at the moment I cannot think of any other examples. This is probably only here because the committee were mostly just reusing existing syntax. –  Richard Corden May 29 '09 at 12:47

 

你可能感兴趣的:(MSVC与GCC的差异(收集中))