4

My compiler (actually Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)) accepts (compile) that code :

class X {
private:
  int i;
public:
  const X() { cout << "here" << endl; i=0; }
  void f() const {}
  void g() {}
};

int main() {
  const X x;
  x.f();
  //  x.g();
  X y;
  y.f();
  y.g();
}

It works as if there is no const qualifier leading the ctor definition. I tried -Wall, -pedantic different kind of standard's activations, always the same... So :

  • did I missed something ? I wasn't able to find that it is syntactically correct in the latest standard…
  • is this a bug of gcc/llvm ? It seems that gcc/llvm silently ignore the const.
  • is this a feature that I missed, and for which my example is not able to demonstrate its usefulness?

Note: gcc 3.4.3 don't compile it, nor gcc 4.4.5.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Hmm that doesn't seem to allow exception-specifications. It is different in the latest draft for C++1y; which explicitly allows exception-specifications and attributes, but still seems not to allow type-specifiers such as `const`. – dyp Nov 12 '13 at 17:43
  • 2
    Submitted a [bugreport](http://llvm.org/bugs/show_bug.cgi?id=17898). – Ali Nov 12 '13 at 18:34

1 Answers1

3

As far as I can tell this does not look like valid syntax, the draft C++ standard section 12.1 Constructors paragraph 1 says:

Constructors do not have names. A special declarator syntax is used to declare or define the constructor. The syntax uses:

— an optional decl-specifier-seq in which each decl-specifier is either a function-specifier or constexpr,

— the constructor’s class name, and

— a parameter list

and we can see from section 7.1.2 Function specifiers are as follows:

function-specifier:
 inline
 virtual
 explicit

Via Ali in the comment above a bug report was filed for this it was confirmed and resolved.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • right! I misread it! In fact I was lost in *ptr-declarator* definition and I missed the *function-specifier*. My version of the draft seems not to be exactly the same as yours. Thanks! One more: "would you qualify it a bug or an unknown g++ feature?" – Jean-Baptiste Yunès Nov 12 '13 at 17:50
  • 1
    @Jean-BaptisteYunès I added a link to the version I am using and I checked out an older version and the language is pretty similar. this [previous thread](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) is a good resource for different versions of the standard. – Shafik Yaghmour Nov 12 '13 at 17:54