Topic: Gnu constructor expressions valid?
Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 9 Aug 2001 20:30:09 CST Raw View
Matvei Brodski wrote:
...
> I am not sure I understand. I thaught that the code like this:
>
> f = (foo){x,x};
>
> should mean: create temporary struct of type foo, initialize it
> with {x,x} and assign it to f. I.e., something that translates into:
>
> foo temp = {x,x};
> f = temp;
>
> This seems ok to me. What am I missing?
You're missing the fact that there is no such feature in C90, or C++.
C99 did add such a feature - such constructs are called compound
literals. However, it chose a different syntax:
f = (struct foo){x,x};
C++ doesn't have compound literals. However, if they are ever added, the
syntax you're using would probably be allowed, because C++ generally
considers 'foo' to be sufficient to identify the type, it doesn't need
the 'struct' in front of it.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: crooney@panix.com (Christopher Rooney)
Date: Thu, 9 Aug 2001 00:10:09 GMT Raw View
Is the following valid according to the standard, or is it a gnu
extension? Web searching for "contructor expression" only brings up
gnu documentation in this context, as well as scads of pages that
happen to have the two terms. It compiles cleanly in g++ with -ansi
-Wall, FWIW.
Thanks,
Chris Rooney
#include <iostream>
int main(){
struct foo {int a; int b;} f;
int x = 33;
f = (foo){x,x};
cout << f.a << " " << f.b << endl;
return 0;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: Thu, 9 Aug 2001 01:50:39 GMT Raw View
In article <30d943d2.0108081042.4bb0fb50@posting.google.com>,
Christopher Rooney <crooney@panix.com> wrote:
>Is the following valid according to the standard, or is it a gnu
>extension? Web searching for "contructor expression" only brings up
>gnu documentation in this context, as well as scads of pages that
>happen to have the two terms. It compiles cleanly in g++ with -ansi
>-Wall, FWIW.
But what happens when you add -pedantic-error?
>#include <iostream>
>
>int main(){
> struct foo {int a; int b;} f;
> int x = 33;
> f = (foo){x,x};
> cout << f.a << " " << f.b << endl;
> return 0;
>}
The assignment to f uses a features of C99 and is not valid C++
(in fact, it's not valid C99 either, until you add struct foo
instead of just foo ... but of course, C99 doesn't support iostreams...)
--
Greg Comeau Countdown to "export": December 1, 2001
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
Tasty C99/C++ Combo: Dinkumware libs + Comeau Compilers == WOW!!!
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 9 Aug 2001 18:25:57 GMT Raw View
In article <30d943d2.0108081042.4bb0fb50@posting.google.com>,
Christopher Rooney <crooney@panix.com> writes
>Is the following valid according to the standard, or is it a gnu
>extension? Web searching for "contructor expression" only brings up
>gnu documentation in this context, as well as scads of pages that
>happen to have the two terms. It compiles cleanly in g++ with -ansi
>-Wall, FWIW.
>
>Thanks,
>
>Chris Rooney
>#include <iostream>
>
>int main(){
> struct foo {int a; int b;} f;
> int x = 33;
> f = (foo){x,x};
I am pretty certain that is a g++ extension. Indeed, as you do not have
a declared ctor, you cannot even write:
f = foo(x, x);
What you might be able to do is:
foo f1 = {x, x};
but even that, I think, would be an extension because x is not a
constant expression.
> cout << f.a << " " << f.b << endl;
> return 0;
>}
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Matvei Brodski <mbrodski@bear.no_spam.com>
Date: Thu, 9 Aug 2001 18:27:44 GMT Raw View
Greg Comeau wrote:
> In article <30d943d2.0108081042.4bb0fb50@posting.google.com>,
> Christopher Rooney <crooney@panix.com> wrote:
> >Is the following valid according to the standard, or is it a gnu
> >extension? Web searching for "contructor expression" only brings up
> >gnu documentation in this context, as well as scads of pages that
> >happen to have the two terms. It compiles cleanly in g++ with -ansi
> >-Wall, FWIW.
>
> But what happens when you add -pedantic-error?
>
> >#include <iostream>
> >
> >int main(){
> > struct foo {int a; int b;} f;
> > int x = 33;
> > f = (foo){x,x};
> > cout << f.a << " " << f.b << endl;
> > return 0;
> >}
>
> The assignment to f uses a features of C99 and is not valid C++
> (in fact, it's not valid C99 either, until you add struct foo
> instead of just foo ... but of course, C99 doesn't support iostreams...)
I am not sure I understand. I thaught that the code like this:
f = (foo){x,x};
should mean: create temporary struct of type foo, initialize it
with {x,x} and assign it to f. I.e., something that translates into:
foo temp = {x,x};
f = temp;
This seems ok to me. What am I missing?
Matvei
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: crooney@panix.com (Christopher Rooney)
Date: Thu, 9 Aug 2001 20:34:05 GMT Raw View
comeau@panix.com (Greg Comeau) wrote in message news:<9ksq0n$a0e$1@panix3.panix.com>...
> But what happens when you add -pedantic-error?
It reports
foo.cc:6: ANSI C++ forbids constructor-expressions
Which I, of course, should have found out before I wasted other
people's time and bandwidth.
Thanks Greg, and sorry all.
Chris
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Steve Clamage <clamage@eng.sun.com>
Date: Thu, 9 Aug 2001 22:49:41 GMT Raw View
On Thu, 9 Aug 2001, Christopher Rooney wrote:
> Date: Thu, 9 Aug 2001 00:10:09 GMT
> From: Christopher Rooney <crooney@panix.com>
> Newsgroups: comp.std.c++
> Subject: Gnu constructor expressions valid?
>
> Is the following valid according to the standard, or is it a gnu
> extension? Web searching for "contructor expression" only brings up
> gnu documentation in this context, as well as scads of pages that
> happen to have the two terms. It compiles cleanly in g++ with -ansi
> -Wall, FWIW.
>
> Thanks,
>
> Chris Rooney
> #include <iostream>
>
> int main(){
> struct foo {int a; int b;} f;
> int x = 33;
> f = (foo){x,x};
> cout << f.a << " " << f.b << endl;
> return 0;
> }
The line
f = (foo){x,x};
is not valid C++ according to the C++ standard, but it is valid
C99 syntax (the new C standard).
C99 has added "compound literals", whereby you can create an
anonymous temporary struct or array with given values.
Standard C++ is based on the 1989 C standard, and features from
C99 are not automatically part of standard C++.
---
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: Thu, 9 Aug 2001 22:49:21 GMT Raw View
In article <3B72B860.FEF58C94@bear.no_spam.com>,
Matvei Brodski <mbrodski@bear.no_spam.com> wrote:
>Greg Comeau wrote:
>>In article <30d943d2.0108081042.4bb0fb50@posting.google.com>,
>>Christopher Rooney <crooney@panix.com> wrote:
>>>Is the following valid according to the standard, or is it a gnu
>>>extension? Web searching for "contructor expression" only brings up
>>>gnu documentation in this context, as well as scads of pages that
>>>happen to have the two terms. It compiles cleanly in g++ with -ansi
>>>-Wall, FWIW.
>>
>>But what happens when you add -pedantic-error?
>>
>>>#include <iostream>
>>>
>>>int main(){
>>> struct foo {int a; int b;} f;
>>> int x = 33;
>>> f = (foo){x,x};
>>> cout << f.a << " " << f.b << endl;
>>> return 0;
>>>}
>>
>>The assignment to f uses a features of C99 and is not valid C++
>>(in fact, it's not valid C99 either, until you add struct foo
>>instead of just foo ... but of course, C99 doesn't support iostreams...)
>
>I am not sure I understand. I thaught that the code like this:
>
>f = (foo){x,x};
>
>should mean: create temporary struct of type foo, initialize it
>with {x,x} and assign it to f. I.e., something that translates into:
>
>foo temp = {x,x};
>f = temp;
>
>This seems ok to me. What am I missing?
There is no such syntax in C++. The closest you'll get is
foo(x, x), but as Francis Glassborow pointed out, you don't
have a ctor taking two arguments. The (foo){x,x} construct
is a C99 features (C99 == Standard C from 1999), and not a
C++ features. FYI, the C99 feature is called ``compound literals''.
Check out http://www.comeaucomputing.com/techtalk/c99/#compoundliterals
for some examples of it. But again, it's not C++.
--
Greg Comeau Countdown to "export": December 1, 2001
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
Tasty C99/C++ Combo: Dinkumware libs + Comeau Compilers == WOW!!!
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]