MSVC与GCC的差异(收集中)

本文探讨了GCC和MSVC编译器在处理变长数组、临时变量作为函数参数及静态常量定义方面的差异。特别关注了静态常量成员在不同编译器下的行为及其背后的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

03-19
### Microsoft Visual C++ (MSVC) Overview and Usage #### What is MSVC? Microsoft Visual C++ (MSVC) serves as a compiler suite designed to develop applications using the C, C++, and C++/CLI programming languages on Windows platforms[^1]. It includes tools such as compilers, linkers, debuggers, libraries, and other utilities necessary for building software. #### Key Features of MSVC The following features highlight its capabilities: - **Compiler Optimization**: The MSVC compiler provides advanced optimization techniques that enhance performance by reducing code size and improving execution speed. - **Standard Compliance**: Supports modern standards including C++17, C++20, and upcoming versions like C++23 with continuous updates from Microsoft. - **Debugging Tools**: Integrated debugging support allows developers to identify issues efficiently during development cycles through breakpoints, watch variables, call stacks analysis etc.. #### Using Classes in C++ In object-oriented programming within C++, classes are fundamental constructs used to define objects' properties and behaviors. For instance, consider this example where `class C` inherits publicly from both base classes `A` and `B`, while also defining member functions overriding their respective parent implementations: ```cpp #include <iostream> // Base Class Definitions struct A { virtual ~A() {} virtual int virt_func_A() const { return 42; } }; struct B { virtual ~B() {} virtual int virt_func_B() const { return 84; } }; // Derived Class Definition class C : public A, public B { private: int c1; public: explicit C(int value):c1(value){} // Override methods declared virtually above virtual int virt_func_A() const override final{ std::cout << "Called overridden method A\n"; return c1 * 2; } virtual int virt_func_B() const override final{ std::cout << "Called overridden method B\n"; return c1 / 2; } }; int main(){ C obj(10); A* pa=&obj; B* pb=&obj; std::cout<<pa->virt_func_A()<<'\n'; // Calls derived version via pointer-to-base type 'A' std::cout<<pb->virt_func_B()<<'\n'; // Calls derived version via pointer-to-base type 'B' } ``` This demonstrates how polymorphism works when dealing with multiple inheritance scenarios involving different interfaces provided by distinct bases being implemented together into one single entity represented here under name space scope resolution operator(::). §§Related Questions§§ 1. How does MSVC handle template metaprogramming compared to GCC or Clang? 2. Can you explain more about exception handling mechanisms supported specifically inside MSVC environment settings ? 3. Is there any specific guideline regarding memory management practices recommended exclusively towards projects compiled against latest iterations available today over official releases ?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值