Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9256289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:54:20+00:00 2026-06-18T11:54:20+00:00

The following code #include <random> std::mt19937 generator((std::random_device())()); compiles just file with clang: $ clang++

  • 0

The following code

#include <random>
std::mt19937 generator((std::random_device())());

compiles just file with clang:

$ clang++ -c -std=c++0x test.cpp

but fails with gcc:

$ g++ -c -std=c++0x test.cpp 
test.cpp:3:47: erro: expected primary-expression before ‘)’ token

Is that code valid in C++11? Is it a bug in GCC or a extension/bug of Clang?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:54:22+00:00Added an answer on June 18, 2026 at 11:54 am

    gcc is parsing the subexpression (std::random_device())() as a cast to the function type std::random_device(). It helps to look at icc’s error output, which is slightly more informative than gcc’s:

    source.cpp(6): error: cast to type "std::random_device ()" is not allowed
      std::mt19937 generator((std::random_device())());
                              ^
    
    source.cpp(6): error: expected an expression
      std::mt19937 generator((std::random_device())());
                                                    ^
    

    The relevant production is 5.4p2:

    cast-expression:

    • unary-expression
    • ( type-id ) cast-expression

    Since an empty pair of parentheses () is not a unary-expression, this production is unavailable and the compiler should select the production from 5.2p1:

    postfix-expression:

    • […]
    • postfix-expression ( expression-listopt )
    • […]

    where the postfix-expression is (std::random_device()) and the expression-list is omitted.

    I’ve filed http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56239 on gcc bugzilla, and it looks like it should be resolved shortly.

    Note that if you are supplying arguments to the operator() then the compiler is required by 8.2p2 to parse the expression as a cast, even though it is illegal to cast to a function type (if there are multiple arguments, the argument list is parsed as an expression using the 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 correct way to write this (other than using C++11 universal initializer syntax) is to add another layer of parentheses, since a type-id cannot contain outer parentheses:

    ((std::less<int>()))(1, 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code #include <iostream> using namespace std; int main() { const char* const
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
I have the following code: #include <iostream> #define BOOST_TEST_MODULE my test #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_CASE(
Consider the following code: #include <iostream> using namespace std; int main() { int x,
The following noddy test code: #include <iostream> #include <list> #include <boost/any.hpp> #include <boost/foreach.hpp> #include
I used std::variate_generator in the following way in a VS2010 project: #include <random> ...
I have the following code #include <iostream> using namespace std; void WaitForEnter() { while(1)
The following code #include <stdio.h> template <typename T, T v> class Tem { T
I have the following code: #include <stdio.h> #include <pthread.h> #define THREAD_CNT 10 #define ITER
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.