Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

g++

ansi

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?

like image 758
anon Avatar asked May 02 '11 19:05

anon


People also ask

Can I use C++ compiler to compile C code?

If the C++ compiler provides its own versions of the C headers, the versions of those headers used by the C compiler must be compatible. Oracle Developer Studio C and C++ compilers use compatible headers, and use the same C runtime library. They are fully compatible.

What is STD C ++ 0x?

C++0x was the working name for the new standard for C++, adding many language features that I'll cover in this series on C++11. In September 2011, C++0x was officially published as the new C++11 standard, and many compilers now provide support for some of the core C++11 features.

What is Fpermissive error in C++?

The -fpermissive flag causes the compiler to report some things that are actually errors (but are permitted by some compilers) as warnings, to permit code to compile even if it doesn't conform to the language rules. You really should fix the underlying problem.


1 Answers

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.)

like image 167
zwol Avatar answered Nov 15 '22 18:11

zwol