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 

How does C++ infer this in compilation?

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





PostPosted: Sun Nov 28, 2004 6:10 pm    Post subject: How does C++ infer this in compilation? Reply with quote



Hi,

How does C++ infer an expression is an implicit initialization or not?
For example, for the class BinaryNumber, which uses a boolean vector
to represent a binary number.

class BinaryNumber
{
public:
BinaryNumber(int n) { data.resize(n); }
...
private:
vector<bool> data;
}

The implicit initialization
BinaryNumber num = 5;
is allowed. How does C++ compilers figure this out?

Thanks a lot!

Andy

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





PostPosted: Sun Nov 28, 2004 11:54 pm    Post subject: Re: How does C++ infer this in compilation? Reply with quote



Andy wrote:
Quote:

The implicit initialization
BinaryNumber num = 5;
is allowed. How does C++ compilers figure this out?

I'm sorry... but I can't see the implicit initialization. According to
§12.6.1/1 it's a case of *explicit* initialization, the code above being
equivalent to

BinaryNumber num(BinaryNumber(5));

Moreover, if certain conditions are met, the compiler is allowed to omit
the temporary and the copy.

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Hopkin
Guest





PostPosted: Mon Nov 29, 2004 9:47 pm    Post subject: Re: How does C++ infer this in compilation? Reply with quote



[email]yyu_mail (AT) yahoo (DOT) com[/email] (Andy) wrote in message news:<c0ec8bb4.0411280535.7acd5c21 (AT) posting (DOT) google.com>...
Quote:
Hi,

How does C++ infer an expression is an implicit initialization or not?
For example, for the class BinaryNumber, which uses a boolean vector
to represent a binary number.

class BinaryNumber
{
public:
BinaryNumber(int n) { data.resize(n); }
...
private:
vector<bool> data;
}

The implicit initialization
BinaryNumber num = 5;
is allowed. How does C++ compilers figure this out?


I'm not quite sure I understand what you're asking, but perhaps this is helpful.

If you declare the constructor explicit:

explicit BinaryNumber(int n) { ... }

then the = form of initialisation is not valid, e.g.

BinaryNumber num = 5;

will not compile, but

BinaryNumber num(5);

will.


James

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