Bug 18000 - g++ interprets a variable declaration as a function prototype when the arguments to the constructor are temporaries
Summary: g++ interprets a variable declaration as a function prototype when the argume...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.2
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 22520 28728 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-10-14 15:08 UTC by Steven Schveighoffer
Modified: 2006-08-14 20:42 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Steven Schveighoffer 2004-10-14 15:08:38 UTC
Platform: x86 (Pentium 4 2.8 GHz with MT)

The following code does not compile, it appears that g++ is interpreting the 
declaration of myvar to be a function prototype:

steves@linux:~> g++3.4.2 -v
Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.2/specs
Configured with: ./configure --program-suffix=3.4.2
Thread model: posix
gcc version 3.4.2
steves@linux:~> g++3.4.2 -Wall -o testcpp testcpp.cpp
testcpp.cpp: In function `int main(int, char**)':
testcpp.cpp:19: error: request for member `m' in `myvar', which is of non-class
type `x ()(y)'
testcpp.cpp:17: warning: unused variable 'v'
testcpp.cpp:19: warning: unused variable 'v2'
steves@linux:~>

The code:

class y
{
public:
        y(int n) : n(n) {}
        int n;
};

class x
{
public:
        x(y m) : m(m) {}
        y m;
};

int main(int argc, char *argv[])
{
        int v = 1;
        x myvar(y(v));
        int v2 = myvar.m.n;
}
Comment 1 Nathan Sidwell 2004-10-14 15:48:08 UTC
This is correct behaviour. [8.2]
Comment 2 Steven Schveighoffer 2004-10-14 19:51:43 UTC
(In reply to comment #1)
> This is correct behaviour. [8.2]

whoops!  I'll be damned.  It doesn't seem like it should be proper behavior :) 
sorry.  Thanks for the quick reply.

If anyone reads this bug, and has a similar problem, the fix for the code 
above is to change the line to:

x myvar = x(y(v));
Comment 3 Andrew Pinski 2005-07-17 15:56:40 UTC
*** Bug 22520 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2006-08-14 20:42:58 UTC
*** Bug 28728 has been marked as a duplicate of this bug. ***