Differences between MSVC++ and G++

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Differences between MSVC++ and G++

Postby sourmìlk » Sun Apr 22, 2012 10:56 am UTC

I had trouble finding this on google: what common pieces of code will compile on MSCV++ but no G++ and visa versa? That is to ask, what are the major differences between the two compilers?
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Differences between MSVC++ and G++

Postby korona » Sun Apr 22, 2012 3:28 pm UTC

They have a different syntax for inline assembler and different builtins. gcc extensions like __attribute__ (( packed )) will obviously not compile in MSVC++ and vice-versa. GCC might have a different syntax for specifying DLL exports and stdcall etc.
When you're only using functions from the C++ standard library there is no difference.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Differences between MSVC++ and G++

Postby EvanED » Sun Apr 22, 2012 4:30 pm UTC

The biggest difference is in name lookup in templates. G++ properly implements the standard "two phase name lookup" while MSVC does not, and delays all name lookup to the point of instantiation. In practice this tends to mean that G++ is stricter than MSVC and if you write code that uses templates and develop with MSVC there's a decent chance you'll have to fix it for G++, but in general you can also have code that compiles with both and has different meanings:

Code: Select all

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
                                                                                                                       
int foo(double d)
{
  return 1;
}

template<typename T>
int bar()
{
    // The standard says that, because the resolution of 'foo'
    // doesn't depend on what template parameter is passed
    // the name is bound at template definition time -- and the
    // only thing in scope now is foo(double). This is what GCC
    // does.
    return foo(1);
}

int foo(int i)
{
    return 2;
}

int main()
{
    // However, MSVC delays name lookup until instantiation
    // time, which is now. And now, 'foo(int)' is in scope -- and
    // that's a better overload, so overload resolution picks it
    // for the 'foo(1)' call.
    //
    // Thus MSVC prints 2 here, while G++ prints 1.
    std::cout << bar<int>() << "\n";
}

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Differences between MSVC++ and G++

Postby sourmìlk » Sun Apr 22, 2012 5:38 pm UTC

Argh, it's one thing for different compilers to interpret ambiguous code differently, but I really don't like that MSVC doesn't meet the standard. Luckily, so far I've been coding for G++ compatibility. Are there any other differences which I'm likely to encounter as problematic?
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Differences between MSVC++ and G++

Postby EvanED » Mon Apr 23, 2012 12:55 am UTC

I don't think so, especially if you do development for both simultaneously. The only other things I can think of are places where MSVC is just a bit more lax than G++ is (e.g. MSVC allows typename to be omitted some places it is strictly necessary, and there are some C++ errors you can convert into warnings with -fpermissive that MSVC always accepts), places where the standard is deliberately unclear (e.g. the contents of typeinfo name() fields -- MSVCs are way easier for humans to read and GCCs are probably more easily machine-readable), and extensions (like korona mentioned).

I wouldn't be surprised if there were a couple other gotchas, but none come to mind.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Differences between MSVC++ and G++

Postby Sc4Freak » Mon Apr 23, 2012 1:22 am UTC

The only other one I can think of that hasn't already been mentioned is that MSVC can be a bit lax on requiring the "template" keyword. This will compile in MSVC10:

Code: Select all

template<typename T, typename TBar>
struct Foo : TBar
{
    typedef Foo<T, typename TBar::Baz<T>> SpecificFoo;
};

struct MyBar
{
    template<typename T>
    struct Baz
    {
    };
};

int main()
{
    Foo<int, MyBar> foo;
}

But (I believe) will fail to compile in GCC. The correct syntax is:

Code: Select all

template<typename T, typename TBar>
struct Foo : TBar
{
    typedef Foo<T, typename TBar::template Baz<T>> SpecificFoo;
};

struct MyBar
{
    template<typename T>
    struct Baz
    {
    };
};

int main()
{
    Foo<int, MyBar> foo;
}


Notice the "TBar::template". But that's probably one of the more obscure gotchas I've come across.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Differences between MSVC++ and G++

Postby sourmìlk » Mon Apr 23, 2012 3:34 am UTC

And that's just more about strictness. If the only problems are that MSVC++ is less strict than G++, then all I have to do to make code that compiles on both is adhere more strictly to the C++ standard.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Differences between MSVC++ and G++

Postby EvanED » Mon Apr 23, 2012 4:06 am UTC

Right. The lack of two-phase lookup can throw a wrench into the works, but in my experience in practice it usually turns into a strictness thing -- because this compiles with MSVC and not GCC (at least, I think; this one is untested)

Code: Select all

template<typename T>
void foo() {
    bar();
}
void bar() {}
... foo<int>();

I partially help work on a fairly large C++ code base that was developed for quite some time under MSVC only then ported to GCC; I don't think we ever saw the hypothetical "silent change of behavior" situation materialize. It was a large effort, but it was fixing compiler errors rather than debugging.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Differences between MSVC++ and G++

Postby sourmìlk » Mon Apr 23, 2012 9:20 pm UTC

I don't like compiler errors. Am I likely to encounter many trying to compile my G++ code in MSVC++?
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Differences between MSVC++ and G++

Postby EvanED » Tue Apr 24, 2012 12:05 am UTC

Nope, not unless you use GCC extensions (pretty obviously) or C++11 stuff (depending on version). I don't recall hitting anything where I had something compile in G++ and not in MSVC.

villadelfia
Posts: 98
Joined: Sat Apr 28, 2007 2:35 am UTC

Re: Differences between MSVC++ and G++

Postby villadelfia » Wed Apr 25, 2012 1:15 am UTC

In general, if it compiles with g++ -Wall -Wextra -Werror -pedantic (-std=c++11) it should compile on everything.

Do note that MSVC is still missing some c++11 features.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Differences between MSVC++ and G++

Postby sourmìlk » Wed Apr 25, 2012 1:32 am UTC

Yeah, it does lack some C++11 features. I altered my code a tiny bit to compile on both (MSVC was confused by the syntax of about two lines of my code) and I'll just use whichever has the most C++11 features enabled at a given time. At the moment, G++ has the ones I need.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests