This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/56239] New: parse error calling operator() on parenthesized value-initialized temporary
- From: "ed at catmur dot co.uk" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 07 Feb 2013 10:37:06 +0000
- Subject: [Bug c++/56239] New: parse error calling operator() on parenthesized value-initialized temporary
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56239
Bug #: 56239
Summary: parse error calling operator() on parenthesized
value-initialized temporary
Classification: Unclassified
Product: gcc
Version: 4.8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: ed@catmur.co.uk
From
http://stackoverflow.com/questions/14732132/global-initialization-with-temporary-function-object
Under gcc 4.8.0 (20130127):
struct S { int operator()(); };
int main() { (S())(); }
source.cpp:2:20: error: expected primary-expression before ')' token
int main() { (S())(); }
^
Motivation for parenthesizing the temporary is to use it in a constructor call:
struct S { int operator()(); };
struct T { T(int); };
int main() { T t((S())()); }
source.cpp:3:24: error: expected primary-expression before ')' token
int main() { T t((S())()); }
^
It appears that gcc is parsing the expression (S())() using production 5.4p2:
cast-expression:
unary-expression
( type-id ) cast-expression
Since an empty pair of parentheses is not a cast-expression, this should
instead be parsed using 5.2p1:
postfix-expression:
[...]
postfix-expression ( expression-list[opt] )
[...]
where the postfix-expression is (S()) and the expression-list is omitted.
Same error occurs in both C++03 and C++11 mode.
icc 13.0.1 has the same issue; clang 3.2 compiles it successfully.
Two workarounds are available: enclose (S()) in an extra pair of parentheses,
or use C++11 universal initialization syntax.