开发者

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

开发者 https://www.devze.com 2023-03-02 14:03 出处:网络
I\'ve come across a really weird error that only pops up if I use the ansi flag. #include <memory>

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(st开发者_运维问答d::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.

0

精彩评论

暂无评论...
验证码 换一张
取 消