GCC vs MSVC

The nightmare never ends… in the beginning writing C++ for the GCC was better than MSVC. The template support was much better. To be exact the C++ compiler of Visual Studio 6 had minimal support for templates. You could do something in GCC and then find out that it doesn’t compile under Visual Studio 6. Back then you had at least the fun of blaming Microsoft for that, which is was always cool…

Now with the Visual Studio .NET the C++ support is SUPERB… to the point that parts are reversed… You code in .NET and pray it also works on GCC. What I had recently… When I was implementing the serialization support for Sylphis I was using MSVC.NET (Actually it is my primary working environment for Sylphis and I often go back to Linux and try if things work there). The code below is a template method that compiles and works fine in .NET…

55:template<class Cont>
56:    void writeContainer(const Cont &cont) {
57:        writeUnsignedInt(cont.size());
58:        Cont::const_iterator i;
59:        for(i = cont.begin() ; i != cont.end() ; ++i){
60:            writeSerializable(*i);
61:        }
62:    }

When I tried GCC (version 3.3.5) yesterday (and a log time since the last try) I got this error :

include/serializer.h:58: error: parse error before `;' token

Isn’t the world of portable code so interesting?

Any ideas?

UPDATE : The problem is solved so don’t let this post confuse you. Check my The rematch post…

  • Leonardo Santagada

    install GCC 3.4.2 or 4.0.1? maybe even mingw/cygwin so you can test everything in windows without having to go to linux to test?

    I am just guessing as I am a python programmer myself.

  • Daniel K. O.

    As Leonardo said, GCC 3.4 has better C++ support than 3.3 (and GCC 4.0 has better support than 3.4, obviously), and you can test it on Windows (at least GCC 3.4.4 is available at MinGW’s website).

    You are probably experiencing a situation where an invalid code is just accepted by the MS compiler and is refused by GCC. This is very common, check out comp.lang.c++ and comp.lang.c++.moderated. Usually people test the code on Comeau C++ (http://www.comeaucomputing.com/tryitout/) and see that GCC isn’t wrong – i.e. it’s common for the code to be Undefined Behaviour, so one possibility is for it to work fine in one compiler, and be rejected by another.

    (I couldn’t find “include/serializer.h” in Sylphis’ CVS to check it out, so I’m just guessing)

  • I’m downloading GCC 4.0 now (it takes a while since I’m online with a 56K modem). I will try it but to tell you the truth I don’t trust GCC 4.0 yet… do you use it on any big project?

  • No luck with GCC 4.0 🙁 I get the same error…

  • Stuart McDonald

    Hi, You need to use typename i.e.

    typename Cont::const_iterator i;

    That should solve it. Cheers,

    STU!

    Debugging is like kicking dead whales down the beach. Slow, painful and generally unplesant.