0

Today when I read C++ Primer ,it said in-class initializer can't use () I have searched on Stackoverflow and find a similar question here.And the accepted answer say:the reason might be that there is a ambiguous between declaration of a member function and definition of a member of type.But I am not complete agree with him.I try the following code:

struct Sales_data
{
    int i(5); //this line can't be regard as a function
};

But the compiler still complain.Who can tell me why.\ compiler:clang++ version:3-4

Community
  • 1
  • 1
icecity96
  • 1,177
  • 12
  • 26

1 Answers1

6

It is disallowed by the language. The reason is that there would be cases where it couldn't be disambiguated from a function declaration:

struct foo
{
  int bar();
};

So instead of replicating the whole most vexing parse fiasco by allowing () to work sometimes, it is outright disallowed.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • More about the reasoning behind this decision can be found in [proposal N2756](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2008/n2756.htm), in `Problem 1` section. –  Apr 04 '15 at 13:55