9

Working on this question, I found an inconsistent behavior.

Why reference binding behave different in a constructor from a common function?

struct A {
};

struct B : public A {
  B(){}
private:
  B(const B&);
};

void f( const B& b ) {}

int main() {
  A a( B() ); // works
  A const & a2 = B(); // C++0x: works, C++03: fails
  f( B() );  // C++0x: works, C++03: fails
}

I have tested it for C++03 with g++-4.1 and Comeau 4.2.45.2 in strict C++03 mode and with C++0x extensions disabled. I got same results.

For C++0x was tested with g++-4.4 and Comeau 4.3.9 in relaxed mode and with C++0x extensions enabled. I got same results.

Community
  • 1
  • 1
Fernando N.
  • 6,369
  • 4
  • 27
  • 30

1 Answers1

16
A a(B());

is the declaration of a function named a returning an A and taking a pointer to a function without argument returning a B. See here. Add parenthesis and you'll get the error you expect:

A a((B()));
Community
  • 1
  • 1
AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • 7
    Famously known as "the most vexing parse". – earl Aug 21 '09 at 08:47
  • I can't wait for the day when I could say to someone "you fell victim to the most vexing parse problem". Of course by the time that happens I'll completely forget about it and return to hating C++. – Idan K Jan 25 '10 at 20:55