How would one go about initializing an unnamed temporary PoD?
To initialize an unnamed PoD:
struct MyStruct
{
int x;
int y;
}
MyStruct func(int a, int b)
{
return MyStruct{a,b};
}
This does not work.
What used to work (on some compilers) was:
return (MyStruct){a,b};
I know it is possible to initialize a named PoD using:
MyStruct s={a,b};
return s;
however this is not what I want.
Is there a way to do this naturally with c++, and if not, what would be
the best way to work around this problem?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
gkoreman (6)
|
5/19/2006 10:11:39 AM |
|
gkoreman@gmail.com wrote:
> How would one go about initializing an unnamed temporary PoD?
>
> To initialize an unnamed PoD:
>
> struct MyStruct
> {
> int x;
> int y;
> }
;
>
> MyStruct func(int a, int b)
> {
> return MyStruct{a,b};
> }
>
> This does not work.
> What used to work (on some compilers) was:
>
> return (MyStruct){a,b};
>
> I know it is possible to initialize a named PoD using:
>
> MyStruct s={a,b};
> return s;
>
> however this is not what I want.
That's the *only* way for you to solve your temporary problem. You
cannot "not want" it. How do you know that it's not what you want
if you don't know what what-you-want looks like?
> Is there a way to do this naturally with c++, and if not, what would
> be the best way to work around this problem?
Initialisation is done by a constructor. Adding a constructor to your
struct will make it non-POD. I am not sure how it's "better", but you
could introduce an "out-of-class constructor":
inline MyStruct makeMyStruct(int a, int b) {
MyStruct s = {a, b};
return s;
}
and then use that function to generate your temporary:
MyStruct func(int a, int b) {
... // supposedly more processing here
return makeMyStruct(a,b);
}
But it is essentially reproducing the same code you "don't want".
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Victor
|
5/20/2006 8:55:29 PM
|
|
gkoreman@gmail.com wrote:
> I know it is possible to initialize a named PoD using:
>
> MyStruct s={a,b};
> return s;
>
> however this is not what I want.
> Is there a way to do this naturally with c++, and if not, what would be
> the best way to work around this problem?
One possibility is to wrap the initialization code in an inline helper
function. For example:
struct foo {
int a;
int b;
};
// helper function
inline foo makefoo( int i, int j )
{
foo f = {i, j};
return f;
}
foo f( int i, int j )
{
return makefoo(i,j);
}
In most cases, I'd expect all the copying to be optimized away, so you
get the effect of in-place initialization of an unnamed temporary.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
johnchx2
|
5/20/2006 9:24:53 PM
|
|
<gkoreman@gmail.com> wrote in message
news:1147977123.028965.14670@i39g2000cwa.googlegroups.com...
> How would one go about initializing an unnamed temporary PoD?
>
> To initialize an unnamed PoD:
>
> struct MyStruct
> {
> int x;
> int y;
> }
>
> MyStruct func(int a, int b)
> {
> return MyStruct{a,b};
> }
>
> This does not work.
> What used to work (on some compilers) was:
>
> return (MyStruct){a,b};
>
> I know it is possible to initialize a named PoD using:
>
> MyStruct s={a,b};
> return s;
>
> however this is not what I want.
> Is there a way to do this naturally with c++, and if not, what would be
> the best way to work around this problem?
The most natural way in C++ is to use a constructor:
struct MyStruct
{
int x;
int y;
MyStruct( int ax, int ay )
:x(ax), y(ay) {}
};
MyStruct func(int a, int b)
{
return MyStruct(a,b);
}
- Dennis
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Dennis
|
5/23/2006 11:02:34 AM
|
|
Yes, this is what I have been doing. I was hoping there was a natural
syntax for it, but this will do just fine. Also, to answer Victor's
question the "this is not what I want" was directed at naming the
variable. There are a lot of places in our code that has:
return (Point){x,y}
or something similar, and it would be a real headache to go and create
named variables in all those places. The inline function will do very
close to what I want. Thank you for your replies.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
gkoreman
|
5/23/2006 11:09:59 AM
|
|
Dennis Jones wrote:
>
> The most natural way in C++ is to use a constructor:
>
> struct MyStruct
> {
> int x;
> int y;
> MyStruct( int ax, int ay )
> :x(ax), y(ay) {}
> };
>
> MyStruct func(int a, int b)
> {
> return MyStruct(a,b);
> }
Adding a constructor to MyStruct makes MyStruct a non-POD, but there are
cases where you would want the PODness. Sometimes I deliberately avoid
constructors to keep something POD.
It's a pity that you have to choose either the array initializer syntax
(and the PODness) or the constructor syntax, but not both. Hopefully we
will have compound literals (anonymous aggregates) as in C99 in the next
version of C++.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Seungbeom
|
5/23/2006 9:52:05 PM
|
|
In article <e4vbkc$55h$1@news.Stanford.EDU>, Seungbeom Kim
<musiphil@bawi.org> wrote:
> Dennis Jones wrote:
> >
> > The most natural way in C++ is to use a constructor:
> >
> > struct MyStruct
> > {
> > int x;
> > int y;
> > MyStruct( int ax, int ay )
> > :x(ax), y(ay) {}
> > };
> >
> > MyStruct func(int a, int b)
> > {
> > return MyStruct(a,b);
> > }
>
> Adding a constructor to MyStruct makes MyStruct a non-POD, but there are
> cases where you would want the PODness. Sometimes I deliberately avoid
> constructors to keep something POD.
>
> It's a pity that you have to choose either the array initializer syntax
> (and the PODness) or the constructor syntax, but not both. Hopefully we
> will have compound literals (anonymous aggregates) as in C99 in the next
> version of C++.
struct MyConstructable:MyStruct
{
MyConstructable(int ax,int bx) {a=ax;b=bx;}
};
MyStruct func(int x,int y)
{
return MyConstructable(x,y);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Carl
|
5/24/2006 3:35:44 PM
|
|
Seungbeom Kim wrote:
> Dennis Jones wrote:
>> The most natural way in C++ is to use a constructor:
>> struct MyStruct
>> {
>> int x;
>> int y;
>> MyStruct( int ax, int ay )
>> :x(ax), y(ay) {}
>> };
>> MyStruct func(int a, int b)
>> {
>> return MyStruct(a,b);
>> }
> Adding a constructor to MyStruct makes MyStruct a non-POD, but
> there are cases where you would want the PODness. Sometimes I
> deliberately avoid constructors to keep something POD.
Me too. POD's have definite advantages when handling order of
initialization issues. Most of the time, I'll define the POD
expressedly for the initialization, and give it a conversion
function to the desired, non-POD type -- the presence of a
member operator T() doesn't prevent PODness.
Another possible solution would be a factory function, something
along the lines of:
MyStruct
createMyStruct( int ax, int ay )
{
MyStruct result = { ax, ay } ;
return result ;
}
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
James
|
5/26/2006 11:47:43 AM
|
|
|
7 Replies
221 Views
(page loaded in 0.147 seconds)
Similiar Articles:7/28/2012 11:13:06 PM
|