2

Please tell me why my program gets compiled and executed when I treat the constructor as a class without private and public sections by just writing the word class before it as follows:

class sample
{
private:
    int a,b;

public:
    class sample(int a1){a = a1;}
};
dlf
  • 9,045
  • 4
  • 32
  • 58
Vaibhav Hiwase
  • 411
  • 1
  • 3
  • 7
  • 6
    This is not valid code. – interjay Jul 24 '14 at 17:53
  • What do you mean by "executed" anyway? What happens? – Ranic Jul 24 '14 at 17:54
  • Your code failed to compile by g++, and it does not seem to be correct c++ code. If you want to know why particular compiler accepts it at least publish your compiler name and version. – Slava Jul 24 '14 at 17:56
  • The code does compile under VS2012. – dlf Jul 24 '14 at 17:56
  • 3
    I think OP understands the code is not correct and is wondering why it compiles anyway (for some compilers, at least). I'm wondering the same thing. – dlf Jul 24 '14 at 17:57
  • *If* it compiled even though the obvious syntax error, what compiler are you using? And what version of it? – Some programmer dude Jul 24 '14 at 17:58
  • @dlf can you try to compile with disabled MS extensions? – Slava Jul 24 '14 at 18:00
  • @Slava It still accepts it. That's also true with the highest warning level enabled. – dlf Jul 24 '14 at 18:02
  • @dlf thanks. How VS treats this code as constructor declaration or inner class? – Slava Jul 24 '14 at 18:06
  • @Slava It interprets it as a constructor (you get "no appropriate default constructor" if you try to instantiate a `sample` without an `int` parameter). – dlf Jul 24 '14 at 18:07
  • Also reproducible in VS2013. – T.C. Jul 24 '14 at 18:07
  • @dlf thanks! I would imagine they just used the same gramma rule for constructor as for class instance declaration ie: `[struct|class] name instance;` but that just a speculation. – Slava Jul 24 '14 at 18:09
  • @user3874009 Consider adding the compiler-bug tag to your question (and editing the title?) since that's what it appears to be. – dlf Jul 24 '14 at 18:28

2 Answers2

5

That isn't valid, and my compiler rejects it. Constructors must be declared using the class name, with no class key (i.e. no class, struct or union keyword).

Perhaps your compiler accepts elaborated type specifiers as constructor names; but if so, that's a non-standard extension, and you shouldn't rely on it. If you're lucky, the compiler might provide an option to disable extensions, which you should use if you don't want to be tied to that compiler for ever.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

The code you've provided violates the spec and technically shouldn't compile:

12.1 Constructors

1 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

in that order. In such a declaration, optional parentheses around the constructor class name are ignored.

(function-specifier means one of inline, virtual, or explicit--see 7.1.2/1--but virtual constructors are disallowed by 12.1/4)

That said, Microsoft Visual C++ 2012 and 2013 (at a minimum) accept it. This appears to be a bug in those compilers.

Update: Microsoft is aware of this issue, but has (understandably) decided not to bother fixing it.

Bug report

Community
  • 1
  • 1
dlf
  • 9,045
  • 4
  • 32
  • 58