MSVC与GCC的差异(收集中)

1. 变长数组的支持.

 

  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

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值