C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template constructor question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Buster
Guest





PostPosted: Mon Aug 07, 2006 9:36 pm    Post subject: template constructor question Reply with quote



{ although it is answered in the FAQ 10.19, I'm letting it in since
there are alternative solutions not necessarily involving copy
initialization, like "Orange o((Lemon()));", for example. -mod }

I'm trying to have a templatized constructor, but when
I use it, it thinks I'm declaring a function:

struct Lemon
{
};

struct Orange
{
template <typename T>
Orange(
const T& t)
{
}

void juice()
{
}
};

void func()
{
Orange o(Lemon());

o.juice(); // error, thinks 'o' is a function
}

If I add another parameter to the constructor,
then it doesn't get confused:

Orange(
const T& t,
int)
....
Orange o(Lemon(), 1);

but I'd prefer fixing this correctly. Is there
some way to do it without this hack ?

Thanks

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alf P. Steinbach
Guest





PostPosted: Tue Aug 08, 2006 12:33 am    Post subject: Re: template constructor question Reply with quote



* Buster:
Quote:
* Victor Bazarov:
{ although it is answered in the FAQ 10.19, I'm letting it in since
there are alternative solutions not necessarily involving copy
initialization, like "Orange o((Lemon()));", for example. -mod }

Uh, yes, Victor, the guidelines say FAQ questions should be rejected,
but I don't think that's been practiced: many postings I've seen would
then have been rejected. And anyway I don't think it should be
practiced too harshly, because the FAQ isn't and can't be 100% perfect:
both you and I have contributed to the FAQ to help Marshall make it more
perfect, but it's an endless quest. So I think that note is OK. ;-)


Quote:
I'm trying to have a templatized constructor,

It doesn't matter that that it's a template constructor, except that as
a template constructor it isn't a copy constructor.

[snip]
Quote:
template <typename T
Orange( const T& t )

[snip]
Quote:
{
Orange o(Lemon());

o.juice(); // error, thinks 'o' is a function
}

Yes, when at all possible, the compiler must treat declarations as
function declarations.

Here's one way around that, the FAQ solution:

Orange o = Orange( Lemon() );

This requires a copy constructor, which you have implicitly.

The way Victor noted, which is the way noted in the standard, IIRC:

Orange o( (Lemon()) );

A variation, which I think can be better because it's more explicit and
doesn't require deep understand of obscure language rules, just Say NO
To Willynilly Heliotropic* Anonymous Temporaries (the "SNOT-WHAT" rule):

Lemon const aLemon;
Orange o( aLemon );

This works because there's no way to interpret it as a function
declaration, since the argument is a value, not a type.


Quote:
If I add another parameter to the constructor,
then it doesn't get confused:

Orange(
const T& t,
int)
...
Orange o(Lemon(), 1);

Oh, but the compiler does get "confused", as you say, or rather,
defaulting to function declaration interpretation, if you /use it in the
same way/, like

Orange o( Lemon(), int() ); // Oops, function declaration.


Quote:
but I'd prefer fixing this correctly. Is there
some way to do it without this hack ?

See above, and note that your "hack" isn't a solution: you just happened
to use the third variation shown above, supplying a value as argument.


*) OK, English is not my native language, and I just couldn't come up
with a word starting with H.

--
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?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.