public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Pollard <andrewp@andypo.net>
To: gcc@gcc.gnu.org
Subject: CVS head (gcc-3.4), problem with new parser?
Date: Tue, 31 Dec 2002 03:42:00 -0000	[thread overview]
Message-ID: <200212311054.gBVAse6v006052@garfield.andypo.net> (raw)


Hi All,

 I've been trying out the new C++ parser on some of our source code
and it threw up a problem that I'm not sure is standard mandated or
not...

Should the following code compile?

a.cxx:
-------------------------
struct A {
       int a, b;
       A(int i, int j) : a(i), b(j) {}
       void foo() {};
};

int
main()
{
	int a = 2;
	A(a, a).foo();

	return (0);
}
--------------------------

% g++34 -v
Reading specs from /usr/local/gcc-3.4-20021231-i686-pc-linux-gnu/lib/gcc-lib/i686-pc-linux-gnu/3.4/specs
Configured with:
Thread model: posix
gcc version 3.4 20021230 (experimental)

% g++34 a.cxx
a.cxx: In function `int main()':
a.cxx:11: error: conflicting types for `A a'
a.cxx:10: error: previous declaration as `int a'
a.cxx:11: error: expected init-declarator
a.cxx:11: error: expected `,' or `;'

It appears to think that I want to declare a new variable 'a' with
type 'A', whereas I want to construct a temporary A(2,2) and invoke
foo() on it. [*]

Previous versions of g++ work (3.0-cvs, 3.2-cvs, 3.3-cvs) and so do
icc70 and VC++6.0

[ (*)

As an aside, I didn't realise until investigating this that things like

    int(a);

actually declare variables.... thus with the following struct

    struct A {
        int a;
        A(int _a = 0) : a(_a) {}
    };

Then

    A(a);

declares a variable called 'a' of type 'A' with value 0 (assuming that
there is no 'a' in scope)

    A(a);

causes a 'redefinition' or 'shadowing' error if there is an 'a' in scope, and

    A(1);

creates and destroys a temporary variable of type 'A' with value 1

which is all rather confusing... Is this what the standard indicates?

In the case that I provided, I think the parser is getting confused
since there are two arguments to A(a, a), so this can never be thought
of as declaring a new variable of type A, it must be a temporary, but
the parser isn't noticing this....

]

Comments?

Andrew.
--
 Andrew Pollard, Brooks-PRI Automation  | home: andrew@andypo.net
670 Eskdale Road, Winnersh Triangle, UK | work: Andrew.Pollard@brooks-pri.com
 Tel/Fax:+44 (0)118 9215603 / 9215660   | http://www.andypo.net

             reply	other threads:[~2002-12-31 10:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-31  3:42 Andrew Pollard [this message]
2002-12-31  3:47 ` Gabriel Dos Reis
2002-12-31  6:36   ` Andrew Pollard
2002-12-31  8:22   ` Andrew Pollard
2002-12-31  9:17 ` Alexandre Oliva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200212311054.gBVAse6v006052@garfield.andypo.net \
    --to=andrewp@andypo.net \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).