Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Empty constructor confusion

86 views
Skip to first unread message

Daniel

unread,
Feb 3, 2014, 11:23:18 PM2/3/14
to
Environment is vs2010sp1 on Windows 7.

Consider the following:

template <typename C>
class A
{
public:
class B;

A()
{
}

A(const A& a)
{
}

void f()
{
std::cout << "Hello world" << std::endl;
}
};

template <typename C>
class A<C>::B : public A<C>
{
public:
B()
{
}
B(size_t n)
{
}
};

If I write

A<char> a(A<char>::B());
a.f();

compilation fails with "error C2228: left of '.f' must have class/struct/union"

But if I change that to

A<char> a(A<char>::B(10));
a.f();

compilation succeeds and the program runs.

Hmmm ... am I doing something wrong?

Thanks,
Daniel


Victor Bazarov

unread,
Feb 3, 2014, 11:41:26 PM2/3/14
to
On 2/3/2014 11:23 PM, Daniel wrote:
> Environment is vs2010sp1 on Windows 7.
>
> Consider the following:
>
> template <typename C>
> class A
> {
> public:
> class B;
>
> A()
> {
> }
>
> A(const A& a)
> {
> }
>
> void f()
> {
> std::cout << "Hello world" << std::endl;
> }
> };
>
> template <typename C>
> class A<C>::B : public A<C>
> {
> public:
> B()
> {
> }
> B(size_t n)
> {
> }
> };
>
> If I write
>
> A<char> a(A<char>::B());

IIRC, it's interpreted as a declaration of a function 'a' that takes a
function [pointer] as an argument and returns A<char>. The argument is
a function with no arguments that returns a A<char>::B. Read up on
"C++ most vexing parse".

The solution is to surround the expression inside the parentheses in
another set of parentheses:

A<char> a((A<char>::B()));

The extra parentheses force the compiler to interpret the inner
expression as an expression instead of a declaration.

> a.f();
>
> compilation fails with "error C2228: left of '.f' must have class/struct/union"
>
> But if I change that to
>
> A<char> a(A<char>::B(10));
> a.f();
>
> compilation succeeds and the program runs.
>
> Hmmm ... am I doing something wrong?

Yes. You didn't read the FAQ before posting. Read the FAQ.

V
--
I do not respond to top-posted replies, please don't ask

Tobias Müller

unread,
Feb 4, 2014, 2:03:34 AM2/4/14
to
How do new users actually know that there is an FAQ and where they can find
it?
(@Daniel, it's: http://www.parashift.com/c++-faq/)

Tobi

Öö Tiib

unread,
Feb 4, 2014, 3:30:36 AM2/4/14
to
On Tuesday, 4 February 2014 09:03:34 UTC+2, Tobias Müller wrote:
> How do new users actually know that there is an FAQ and where they can find
> it?
>
> (@Daniel, it's: http://www.parashift.com/c++-faq/)

Typing "C++ FAQ" into google gives it first. C++ is unfortunately not
too fitting tool for that subset of humankind who can't figure it out
on their own.

Daniel

unread,
Feb 4, 2014, 5:07:50 AM2/4/14
to
On Monday, February 3, 2014 11:41:26 PM UTC-5, Victor Bazarov wrote:
> On 2/3/2014 11:23 PM, Daniel wrote:
>
> Read up on "C++ most vexing parse".
>
Thank you!
>
> > Hmmm ... am I doing something wrong?
>
> Yes. You didn't read the FAQ before posting. Read the FAQ.
>
I am sorry that my efforts to find an answer myself were not successful, and for making you upset.

Best regards,
Daniel

Daniel

unread,
Feb 4, 2014, 5:12:12 AM2/4/14
to
On Tuesday, February 4, 2014 3:30:36 AM UTC-5, Öö Tiib wrote:
>
>
> Typing "C++ FAQ" into google gives it first. C++ is unfortunately not
> too fitting tool for that subset of humankind who can't figure it out
> on their own.

It's curious that nobody seems to be asking questions on this newsgroup anymore. Could it be because everything is now in the FAQ? Or could it be
because ...

Best regards,
Daniel



Tobias Müller

unread,
Feb 4, 2014, 5:47:22 AM2/4/14
to
Sure, if you know that an FAQ exists you should be able to find it. But for
someone that is new to this group or even to the usenet it's not obvious.
Do usenet groups usually have FAQs?

And even if you are able to find it, it may be difficult to see if your
question is actually part of it.

Don't get me wrong, the FAQ is a good thing, but I don't see it as a hard
requirement to read it before posting here.

Tobi

Öö Tiib

unread,
Feb 4, 2014, 7:45:29 AM2/4/14
to
On Tuesday, 4 February 2014 12:47:22 UTC+2, Tobias Müller wrote:
> Öö Tiib <oot...@hot.ee> wrote:
> > On Tuesday, 4 February 2014 09:03:34 UTC+2, Tobias Müller wrote:
> >> How do new users actually know that there is an FAQ and where they can find
> >> it?
> >>
> >> (@Daniel, it's: http://www.parashift.com/c++-faq/)
> >
> > Typing "C++ FAQ" into google gives it first. C++ is unfortunately not
> > too fitting tool for that subset of humankind who can't figure it out
> > on their own.
>
> Sure, if you know that an FAQ exists you should be able to find it. But for
> someone that is new to this group or even to the usenet it's not obvious.
> Do usenet groups usually have FAQs?

Existence of FAQ for every non-silly and for lot of silly topics is a textual
tradition that perhaps started from early mailing lists. For more important
topics there are likely several FAQs.

> And even if you are able to find it, it may be difficult to see if your
> question is actually part of it.

FAQ is helpful source of basic information. There are questions that everybody
will ask sooner or later. Victor's suggestion to read FAQ was not rude (RTFM of
early days of internet), it is fine idea.

> Don't get me wrong, the FAQ is a good thing, but I don't see it as a hard
> requirement to read it before posting here.

I do not argue there. I just said that C++ is unfortunately not too good tool
for people whose mindset is not prepared for a territory full of bear traps
and so do not value maps of it.

For example the very "most vexing parse" of OP. It has been there as
long there was C++. How many of us want to declare a function in body
of other function? Close to zero. It is on close to all cases a typo. Our
attitude is that we know what we are doing so only Clang and only quite
recently has experimentally started to warn about it bit more clearly.
It is hard road to find all such traps with trial and error.

Victor Bazarov

unread,
Feb 4, 2014, 7:51:25 AM2/4/14
to
Upset? Don't be silly. Frustration about inability to solve one's
problem is often perceived by one as the most irritating, and some even
see the need to ask for help as a sign of weakness. It cannot be
helped, however, since it's all in one's head. And as they always say,
if you've gained nothing, you at least have gained experience.
Searching for answers elsewhere (which you supposedly have done) helps
you develop and hone those investigative skills that you will use in
your life and work for the years to come, I am sure.

And now you know that there is such a thing as FAQ. Next time perhaps
you will also look for FAQ in absence of a direct answer to your
question, no matter what the venue.

Best of luck!

Daniel

unread,
Feb 4, 2014, 8:19:57 AM2/4/14
to
On Tuesday, February 4, 2014 7:51:25 AM UTC-5, Victor Bazarov wrote:
>
> Best of luck!
>
Thank you. And best of luck to you with your social skills!

Daniel

Pierre Du Bois

unread,
Feb 4, 2014, 8:22:29 AM2/4/14
to
If you won't bother answer in a few lines, why don't you shut up and leave
in silence. One must be a complete idiot to read a FAQ.

Victor Bazarov

unread,
Feb 4, 2014, 8:38:03 AM2/4/14
to
On 2/4/2014 8:22 AM, Pierre Du Bois wrote:
>[..]
> If you won't bother answer in a few lines, why don't you shut up and leave
> in silence.

It most likely due to a completely different reason that you have to
reply in that manner.

> One must be a complete idiot to read a FAQ.

Figures.

Stuart

unread,
Feb 4, 2014, 8:50:36 AM2/4/14
to

On February 3, 2014, Victor Bazarov wrote:
>> Read up on "C++ most vexing parse".

That should have been all of Victor's answer.

On 02/04/14, Daniel wrote:
> Thank you!

And that should have been your reply...


Daniel wrote:
>>> Hmmm ... am I doing something wrong?
Victor wrote:
>> Yes. You didn't read the FAQ before posting. Read the FAQ.

IMNSHO, that advice is bullshit. After all, Daniel encountered some
really disgusting compiler behaviour (I'd even call it a design error of
the C++ language). How was he to know that the problem he encountered
has already a name? Should he comb through the whole FAQ just to find
out that his problem is already in there? And be honest, is this really
a question that is so frequently asked in the newsgroup that it deserves
a place in the FAQ? I think that Daniel's question was well worth
reading and brought some information back from long-term memory.

No offence, Victor. Your answer was - as usually - very informative.

Regards,
Stuart

Daniel

unread,
Feb 4, 2014, 9:08:17 AM2/4/14
to
On Tuesday, February 4, 2014 8:50:36 AM UTC-5, Stuart wrote:
> On February 3, 2014, Victor Bazarov wrote:
>
> >> Read up on "C++ most vexing parse".
>
> That should have been all of Victor's answer.
>
> On 02/04/14, Daniel wrote:
>
> > Thank you!

> And that should have been your reply...
>
Stuart, you're absolutely right, I should have ended it at that. I shouldn't have let Victor's condescending tone affect my appreciation for his quick response. I've followed his posts for many years, and I know he's always like that. The fact remains that he is very helpful, and it is appreciated.

My apologies,
Daniel

Victor Bazarov

unread,
Feb 4, 2014, 9:18:47 AM2/4/14
to
On 2/3/2014 11:23 PM, Daniel wrote:
> [..]
> Consider the following:
> [..]

http://www.parashift.com/c++-faq/fn-decl-vs-obj-instantiation.html

Öö Tiib

unread,
Feb 4, 2014, 10:08:51 AM2/4/14
to
On Tuesday, 4 February 2014 15:50:36 UTC+2, Stuart wrote:
> And be honest, is this really
> a question that is so frequently asked in the newsgroup that it deserves
> a place in the FAQ?

Isn't it a bear trap that everybody should know of?

Most C++ developers worth mentioning as such have met the issue in
various forms and it deserves special name. Worst case of it is when
someone thinks that he is taking some scoped lock or critical section
when actually declaring a function. Then error is there silently waiting
for race condition. It was one of the reasons behind yet another
initialisation syntax 'Type variable{stuff, fluff};' introduced by C++11.

Daniel

unread,
Feb 4, 2014, 10:37:25 AM2/4/14
to
On Tuesday, February 4, 2014 10:08:51 AM UTC-5, Öö Tiib wrote:
> On Tuesday, 4 February 2014 15:50:36 UTC+2, Stuart wrote:
>
>> And be honest, is this really
>> a question that is so frequently asked in the newsgroup that it
>> deserves a place in the FAQ?
>
Lots of web sites have a FAQ before a single question has been asked :-)
>
> Isn't it a bear trap that everybody should know of?
>
Yep. I knew about it 15-20 years ago. But in the context in which I was
working, I just didn't see it.

Daniel
0 new messages