Empty constructor confusion

D

Daniel

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
 
V

Victor Bazarov

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
 
D

Daniel

On 2/3/2014 11:23 PM, Daniel wrote:

Read up on "C++ most vexing parse".
Thank you!


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
 
D

Daniel

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
 
T

Tobias Müller

Öö Tiib said:
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?

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

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.
 
V

Victor Bazarov

I am sorry that my efforts to find an answer myself were not successful, and for making you upset.

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!

V
 
P

Pierre Du Bois

Victor said:
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!

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.
 
V

Victor Bazarov

[..]
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.

V
 
S

Stuart

That should have been all of Victor's answer.

Thank you!

And that should have been your reply...


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
 
D

Daniel

That should have been all of Victor's answer.
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 quickresponse. 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
 
Ö

Öö Tiib

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.
 
D

Daniel

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
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top