|
Prev: throw msg macro
Next: Allocating memory with "new"
From: Sard on 20 Apr 2008 18:03 I've just discovered that this is legal in c++. int x={10}; What's the point of allowing this? Is it just to be consistent with the initialisation of structs and arrays? Are these 3 all equivalent then? int x(10); int x =10; int x ={10};
From: Barry Schwarz on 20 Apr 2008 23:59 On Sun, 20 Apr 2008 15:03:24 -0700 (PDT), Sard <Sardaukary(a)gmail.com> wrote: >I've just discovered that this is legal in c++. > >int x={10}; > >What's the point of allowing this? Is it just to be consistent with >the initialisation of structs and arrays? What is the point of disallowing it and forcing compiler writers to do the additional checking? > >Are these 3 all equivalent then? > >int x(10); This is a syntax error. >int x =10; >int x ={10}; These two are equivalent. Remove del for email
From: Ian Collins on 21 Apr 2008 00:02 Barry Schwarz wrote: > On Sun, 20 Apr 2008 15:03:24 -0700 (PDT), Sard <Sardaukary(a)gmail.com> > wrote: > >> I've just discovered that this is legal in c++. >> >> int x={10}; >> >> What's the point of allowing this? Is it just to be consistent with >> the initialisation of structs and arrays? > > What is the point of disallowing it and forcing compiler writers to do > the additional checking? > >> Are these 3 all equivalent then? >> >> int x(10); > > This is a syntax error. > Not in C++. -- Ian Collins.
From: Ron Natalie on 23 Apr 2008 08:17 Barry Schwarz wrote: >> int x(10); > > This is a syntax error. Not in C++ it isn't.
From: Jerry Coffin on 4 May 2008 15:33 In article <01fe3f54-7898-485e-8f09-706a2f351b47 @s50g2000hsb.googlegroups.com>, Sardaukary(a)gmail.com says... > I've just discovered that this is legal in c++. > > int x={10}; > > What's the point of allowing this? Is it just to be consistent with > the initialisation of structs and arrays? > > Are these 3 all equivalent then? > > int x(10); > int x =10; > int x ={10}; Sort of. In theory, the second is a bit different from the first. The first directly initializes x with the value 10. The second initializes a temporary with the value 10, then uses the copy ctor to copy that value into x. In the case of type int, this doesn't really make any difference -- but for a user defined type, it can. Even though the compiler can (and usually will) elide the use of the copy constructor, it still has to be available. If you defined a class with a public ctor that took an int, but a private copy constructor, the first would work but the second would not. The third is only allowed for aggregates -- basically PODs, though the two are technically separate. Consider: class X { int x; X(X const &); public: X(int value) : x(value) {} }; int main() { X x(10); // allowed. X x = 10; // Fails: copy ctor isn't accessible. X x = {10}; // Fails: brace initialization only for aggregates. return 0; } Quite a few compilers will let you by with the second (especially if you don't ask for full compliance) since they'll normally eliminate the copy construction. I'm not aware of any compilers that allow the third, though I'll admit I haven't tried to look for them. -- Later, Jerry. The universe is a figment of its own imagination.
|
Pages: 1 Prev: throw msg macro Next: Allocating memory with "new" |