 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andy Guest
|
Posted: Sun Nov 28, 2004 6:10 pm Post subject: How does C++ infer this in compilation? |
|
|
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
|
Posted: Sun Nov 28, 2004 11:54 pm Post subject: Re: How does C++ infer this in compilation? |
|
|
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
|
Posted: Mon Nov 29, 2004 9:47 pm Post subject: Re: How does C++ infer this in compilation? |
|
|
[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 |
|
 |
|
|
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
|
|