简体   繁体   中英

Why does my C++0x code fail to compile if I include the “-ansi” compiler option?

I've come across a really weird error that only pops up if I use the ansi flag.

#include <memory>

class Test
{
  public:
    explicit Test(std::shared_ptr<double> ptr) {}
};

Here's the compilation, tested with gcc 4.5.2 and 4.6.0 (20101127):

g++ -std=c++0x -Wall -pedantic -ansi test.cpp
test.cpp:6:34: error: expected ')' before '<' token

But compiling without -ansi works. Why?

For the GNU C++ compiler, -ansi is another name for -std=c++98 , which overrides the -std=c++0x you had earlier on the command line. You probably want just

$ g++ -std=c++0x -Wall minimal.cpp

( -pedantic is on by default for C++, so it's unnecessary to say it again. If you want pickier warnings, try adding -Wextra .)

std::shared_ptr doesn't exist in c++98. Try these changes:

#include <tr1/memory>
...
explicit Test(std::tr1::shared_ptr<double> ptr) {}   

Um, because there is not yet an ANSI standard for C++0x? The ANSI flag checks for conformance with existing standards, not future ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM