c++ - Global initialization with temporary function object -
the next code
#include <random> std::mt19937 generator((std::random_device())());
compiles file clang:
$ clang++ -c -std=c++0x test.cpp
but fails gcc:
$ g++ -c -std=c++0x test.cpp test.cpp:3:47: erro: expected primary-expression before ‘)’ token
is code valid in c++11? bug in gcc or extension/bug of clang?
gcc parsing subexpression (std::random_device())()
cast function type std::random_device()
. helps @ icc's error output, more informative gcc's:
source.cpp(6): error: cast type "std::random_device ()" not allowed std::mt19937 generator((std::random_device())()); ^ source.cpp(6): error: expected look std::mt19937 generator((std::random_device())()); ^
the relevant production 5.4p2:
cast-expression:
unary-expression ( type-id ) cast-expressionsince empty pair of parentheses ()
not unary-expression, production unavailable , compiler should select production 5.2p1:
postfix-expression:
[...] postfix-expression ( expression-listopt ) [...]where postfix-expression (std::random_device())
, expression-list omitted.
i've filed http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56239 on gcc bugzilla, , looks should resolved shortly.
note if supplying arguments operator()
compiler required 8.2p2 parse look cast, though illegal cast function type (if there multiple arguments, argument list parsed expression using comma operator:
(std::less<int>())(1, 2); ^~~~~~~~~~~~~~~~~~ illegal c-style cast ^~~~~~~~~~~~~~~~ type-id of function type std::less<int>() ^~~~~~ argument of c-style cast ^ comma operator
the right way write (other using c++11 universal initializer syntax) add together layer of parentheses, since type-id cannot contain outer parentheses:
((std::less<int>()))(1, 2);
c++ gcc c++11 clang
No comments:
Post a Comment