 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mimi Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Some doubts about Faq 10.19 |
|
|
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();
} |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: Some doubts about Faq 10.19 |
|
|
mimi wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: | 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 |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: Some doubts about Faq 10.19 |
|
|
mimi wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: Some doubts about Faq 10.19 |
|
|
* mimi:
| Quote: | 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
mimi Guest
|
Posted: Wed Nov 22, 2006 10:10 am Post subject: Re: Some doubts about Faq 10.19 |
|
|
"Kai-Uwe Bux дµÀ£º
"
| Quote: | mimi wrote:
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.
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.
| Quote: | 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 |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|