11

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
anon
  • 121
  • 4

3 Answers3

10

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

zwol
  • 135,547
  • 38
  • 252
  • 361
  • Thanks, I've been using the same options for such a long time and haven't thought about exactly what `-ansi` did, and just started using the new standard's features. – anon May 02 '11 at 20:03
  • 1
    _"`-pedantic` is on by default for C++"_ Is it? – Lightness Races in Orbit Oct 05 '15 at 18:48
  • @LightnessRacesinOrbit Yes, the documentation is a little unclear on this point but it's been that way for as long as I can remember. It would be more accurate to say that `-pedantic-errors` is the default in C++, and `-fpermissive` mode is approximately the same as `-pedantic` for C; I'm not sure it's even *possible* to disable those warnings in C++. – zwol Oct 05 '15 at 19:08
  • 1
    https://gcc.gnu.org/onlinedocs/gcc/Standards.html _"to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings)."_ Seems pretty clear to me. And my experience is that adding `-pedantic` produces more warnings. So I must disagree with you. – Lightness Races in Orbit Oct 05 '15 at 19:15
  • @LightnessRacesinOrbit If that is so, I'm pretty sure it's unintentional and you should file a bug report. – zwol Oct 05 '15 at 20:05
  • @zwol: I just quoted the official documentation that suggests otherwise, no? – Lightness Races in Orbit Oct 05 '15 at 21:24
  • @LightnessRacesinOrbit I have reason to believe that is an error in the documentation (specifically, I think the text in the C++ section was copied and pasted from the C section by someone who was unfamiliar with how the C++ compiler was supposed to behave). This conversation is not going to convince me to change my answer. Instead, please raise this question on gcc-help@gcc.gnu.org, and if *they* say I'm mistaken *then* I'll revise it. – zwol Oct 05 '15 at 21:36
  • @zwol: I really don't understand where you're coming from. Perhaps [this](http://coliru.stacked-crooked.com/a/5f8d664fcf63b198) and [this](http://coliru.stacked-crooked.com/a/b69a5790fd197cbd) will make things clearer? AFAICT, `-pedantic` has never been default and has never been a no-op. The documentation and the given examples agree with my statements. I can find none to back up yours. So claiming that the whole lot of observations must be a bug... seems daft! OTOH, if my _links to live demonstrations_ aren't enough to convince you, then I guess we're done here........ – Lightness Races in Orbit Oct 05 '15 at 21:47
  • @LightnessRacesinOrbit Yes, I really do believe that every last one of your anecdotes reflects a bug in the compiler or its documentation, because *I believe I know how this is **supposed** to work,* based on, well, I used to work directly with the guy who wrote GCC's C++ parser. This is why I keep saying that you should bring this up with gcc-help@ and/or their bug tracker. – zwol Oct 05 '15 at 21:55
  • @zwol: I'll pass. I'm satisfied that my experience with this product over the years combined with the observable behaviour and the documentation are consistent and logical enough not to be concerned that they may not be deliberate. And, frankly, even if this _is_ considered a bug by someone who used to know some guy who used to work on GCC, I'm glad it hasn't been fixed because the _actual_ observable behaviour makes complete sense! That takes us back to my original comment: no matter the intent, your statement at the end of this answer is currently _wrong_. :) – Lightness Races in Orbit Oct 05 '15 at 22:03
3

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

#include <tr1/memory>
...
explicit Test(std::tr1::shared_ptr<double> ptr) {}   
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

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