How do I get g++ to compile c++11 code with a move constructor?

29,806

Solution 1

Say g++ -std=c++0x ./t2.cpp.

While you're at it, you might as well Do It Right and enable all warnings:

g++ -W -Wall -Wextra -pedantic -std=c++0x -o t2 t2.cpp

You really, really shouldn't be compiling with any less, especially if you are going to ask questions about your code on SO :-) Various optimization flags should optionally be considered for the release version, such as -s -O2 -flto -march=native.

Solution 2

You probably forgot to add -std=c++0x to your commandline.

Share:
29,806
Talia
Author by

Talia

Hey! Welcome to my personal StackExchange profile! As of this writing (end of November 2020), I am Staff Software Engineer at Oscar Health Insurance. However, almost all of my questions and answers on StackOverflow are from much earlier in my career.... Thank you to the wonderful community here for helping me to get where I am today. LinkedIn GitHub

Updated on October 26, 2020

Comments

  • Talia
    Talia over 3 years

    I can't seem to get g++ to compile c++11 code that uses a move constructor. I keep getting this error:

    collin@Serenity:~/Projects/arraylib$ g++ ./t2.cpp
    ./t2.cpp:10:27: error: expected ‘,’ or ‘...’ before ‘&&’ token
    ./t2.cpp:10:38: error: invalid constructor; you probably meant ‘Blarg (const Blarg&)’
    

    The program I am writing is quite different from this, but I trimmed it down to the part that seems like it should definitely work, yet still triggers the error:

    #include <iostream>
    
    using namespace std;
    
    class Blarg {
        public:
            Blarg () {};
            Blarg (const Blarg& original) {}; /* Copy constructor */
            Blarg (Blarg&& original) {}; /* Move constructor */
    };
    
    int main(int argc, char *argv[])
    {
        Blarg b;
        return 0;
    }
    

    Can anyone tell me what I am doing wrong? Rather, how to fix it?

    This is my gcc version:

    gcc (Ubuntu/Linaro 4.6.2-14ubuntu2) 4.6.2
    
  • Captain Giraffe
    Captain Giraffe about 12 years
    "Do It Right", while the man page for -std=c++0x states "This option enables experimental features that may be removed in future" They sure are defensive in those manpages :-)
  • Talia
    Talia about 12 years
    Thanks so much for saving me from filtering through the 600+ page manual... I'll just double check that this works the way I intended and then mark this as the answer.
  • Kerrek SB
    Kerrek SB about 12 years
    @CaptainGiraffe: To be sure, the dialect option isn't subsumed under "doing it right". That said, c++0x will be supported for some time, but from 4.7 onward you can say c++11, too.
  • Captain Giraffe
    Captain Giraffe about 12 years
    Ooh, can't wait to put that switch into action. Christmas parcels comes early this year=)
  • Philipp
    Philipp about 12 years
    Unfortunately, even -Wall -Wextra -Werror doesn't enable all warnings (see stackoverflow.com/questions/4661561 and stackoverflow.com/questions/399850)
  • Admin
    Admin about 10 years
    @Philipp That's fortunate, not unfortunate. For an extreme example: do you really want a warning for every variable you declare, ever, anywhere in your program, even the ones implicitly generated by the compiler? (-Wlarger-than=1) For a less extreme example: do you really want a warning when you use, for instance, long long, when you already specify -std=c++11 on the command-line to enable new C++11 language features such as that one? (-Wlong-long)
  • Fantastic Mr Fox
    Fantastic Mr Fox about 10 years
    Poor thing, same answer time with just as good relevant answer but not as many up votes. I think yours is a more succinct relevant answer!