public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Anonymous struct
@ 2010-04-07 14:20 John S. Fine
  2010-04-07 15:46 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: John S. Fine @ 2010-04-07 14:20 UTC (permalink / raw)
  To: GCC Help Mailing List

Can someone with a good understanding of the C++ standard tell me if 
this program is legal C++

#include <iostream>
int x = 1;
int main(int argc, char** argv)
{
    struct {
        int operator()( int y ) { return x + y; }
    } fun;

    std::cout << fun( 7 ) << std::endl;
    return 0;
}

It compiles and runs with gcc 4.4.1.  I have done similar things in 
other situations many times, so I assumed that it was valid.

But another compiler (not a version of GCC) reports:

error C2627: member function defined in unnamed class

I hope that other compiler is just wrong.  But I'd like someone with a 
better understanding of the standard to comment.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Anonymous struct
  2010-04-07 14:20 Anonymous struct John S. Fine
@ 2010-04-07 15:46 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2010-04-07 15:46 UTC (permalink / raw)
  To: John S. Fine; +Cc: GCC Help Mailing List

"John S. Fine" <johnsfine@verizon.net> writes:

> Can someone with a good understanding of the C++ standard tell me if
> this program is legal C++
>
> #include <iostream>
> int x = 1;
> int main(int argc, char** argv)
> {
>    struct {
>        int operator()( int y ) { return x + y; }
>    } fun;
>
>    std::cout << fun( 7 ) << std::endl;
>    return 0;
> }
>
> It compiles and runs with gcc 4.4.1.  I have done similar things in
> other situations many times, so I assumed that it was valid.
>
> But another compiler (not a version of GCC) reports:
>
> error C2627: member function defined in unnamed class
>
> I hope that other compiler is just wrong.  But I'd like someone with a
> better understanding of the standard to comment.

You are not permitted to define a member function in an anonymous
union, but I don't see any such restriction for an anonymous class or
struct.

Ian

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-04-07 15:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-07 14:20 Anonymous struct John S. Fine
2010-04-07 15:46 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).