Some doubts about Faq 10.19

M

mimi

Hi, guys.
I am reading the C++ faq this days. When I am reading the faq 10.19, i
am not quite sure about it. I tested the code in my VC 6.0, and it
works fine. Who is right? The faq author or the comiler? My personal
preference would be the author. But could someone explain the issue
more clearly for me? I do not quite understand the explanation in the
faq, especially this sentece "When the compiler sees Foo x(Bar()), it
thinks that the Bar() part is declaring a non-member function that
returns a Bar object, so it thinks you are declaring the existence of a
function called x that returns a Foo and that takes as a single
parameter of type "non-member function that takes nothing and returns a
Bar."
The faq is http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
My code for test:
#include <iostream>
using namespace std;

class Bar {
public:
Bar() {}
};

class Foo {
public:
Foo(const Bar& b) {} // or perhaps Foo(Bar b)
void blah() {std::cout << "hi\n";}
};

int main()
{
Foo x(Bar());
x.blah();
}
 
K

Kai-Uwe Bux

mimi said:
Hi, guys.
I am reading the C++ faq this days. When I am reading the faq 10.19, i
am not quite sure about it. I tested the code in my VC 6.0, and it
works fine. Who is right? The faq author or the comiler?

The FAQ. It appears that VC 6.0 is broken.
My personal
preference would be the author. But could someone explain the issue
more clearly for me? I do not quite understand the explanation in the
faq, especially this sentece "When the compiler sees Foo x(Bar()), it
thinks that the Bar() part is declaring a non-member function that
returns a Bar object, so it thinks you are declaring the existence of a
function called x that returns a Foo and that takes as a single
parameter of type "non-member function that takes nothing and returns a
Bar."

That sounds correct.
The faq is http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
My code for test:
#include <iostream>
using namespace std;

class Bar {
public:
Bar() {}
};

class Foo {
public:
Foo(const Bar& b) {} // or perhaps Foo(Bar b)
void blah() {std::cout << "hi\n";}
};

int main()
{
Foo x(Bar());
x.blah();
}

This should not compile. You can try your code at:

http://www.comeaucomputing.com/tryitout


Best

Kai-Uwe Bux
 
S

Salt_Peter

mimi said:
Hi, guys.
I am reading the C++ faq this days. When I am reading the faq 10.19, i
am not quite sure about it. I tested the code in my VC 6.0, and it
works fine. Who is right? The faq author or the comiler? My personal
preference would be the author. But could someone explain the issue
more clearly for me? I do not quite understand the explanation in the
faq, especially this sentece "When the compiler sees Foo x(Bar()), it
thinks that the Bar() part is declaring a non-member function that
returns a Bar object, so it thinks you are declaring the existence of a
function called x that returns a Foo and that takes as a single
parameter of type "non-member function that takes nothing and returns a
Bar."
The faq is http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
My code for test:
#include <iostream>
using namespace std;

class Bar {
public:
Bar() {}
};

class Foo {
public:
Foo(const Bar& b) {} // or perhaps Foo(Bar b)
void blah() {std::cout << "hi\n";}
};

int main()
{
Foo x(Bar());
x.blah();
}

I wouldn't be surprised that VC6 accepts it, the question remains
whether it accepts it within norms. g++ definitely fails it with the
description provided.
I like his explanation, he is saying that Foo x(...) is declaring a new
function. And it is!

Foo x(const Bar& r_bar)
{
std::cout << "function Foo x(...)\n";
return Foo(r_bar);
}

int main()
{
Foo x(Bar());
x(Bar()).blah();
}

/*
Bar()
function Foo x(...)
Foo::blah()
*/ ewww
 
A

Alf P. Steinbach

* mimi:
Hi, guys.
I am reading the C++ faq this days. When I am reading the faq 10.19, i
am not quite sure about it. I tested the code in my VC 6.0, and it
works fine. Who is right? The faq author or the comiler? My personal
preference would be the author. But could someone explain the issue
more clearly for me? I do not quite understand the explanation in the
faq, especially this sentece "When the compiler sees Foo x(Bar()), it
thinks that the Bar() part is declaring a non-member function that
returns a Bar object, so it thinks you are declaring the existence of a
function called x that returns a Foo and that takes as a single
parameter of type "non-member function that takes nothing and returns a
Bar."
The faq is http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
My code for test:
#include <iostream>
using namespace std;

class Bar {
public:
Bar() {}
};

class Foo {
public:
Foo(const Bar& b) {} // or perhaps Foo(Bar b)
void blah() {std::cout << "hi\n";}
};

int main()
{
Foo x(Bar());
x.blah();
}

VC6 is a very old compiler, not quite standard-conforming.

Later versions of Visual C++ do not accept that code.

As long as Greg Comeau is willing to provide that service, you can
always test code like that with Comeau Online, an online version of the
Comeau C++ compiler (probably the most standard-conforming one); of
course it also rejects the above.
 
M

mimi

"Kai-Uwe Bux дµÀ£º
"
The FAQ. It appears that VC 6.0 is broken.
You are right, I tried it in VC2005, and it can't be compiled. I think
i should go to my lovely gcc.Thank you for your reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top