简体   繁体   中英

how to initialize std::mt19937

I'm getting a compilation error for these two specific lines of code. If I do exactly as the compilers asks me to, then I would end up with a behaviour that is different from the one that I want.

If it's any indication, I am using clang to compile :

~ $ g++ -v      
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.12)
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

These are the two lines of code from which the error comes from.

std::random_device rd;
std::mt19937 gen{rd()};

This is the compiling call.

g++ -O3 -fno-tree-vectorize src/comp.cpp src/common.h src/internal.cpp src/tsc_x86.h src/main.cpp

This is the compiler error.

src/main.cpp:73:21: error: expected ';' at end of declaration
    std::mt19937 gen{rd()};
                    ^
                    ;
src/main.cpp:89:21: error: expected ';' at end of declaration
    std::mt19937 gen{rd()};
                    ^
                    ;

If it's any indication, this is for a homework (although debugging the template file was not listed as a task).

Clang builds as C++98 by default.

Pass -std=c++17 to get yourself up-to-date and gain access tonewer syntax .

Currently, it doesn't recognise this initialization syntax, which is why all it can think to suggest is the addition of a semicolon (and, you're right: that would not actually be what you intended).

If you're wondering why referencing the C++11 feature std::mt19937 worked anyway, that'll either be because it's a library feature (which they may not bother disabling in earlier standard modes), or since you never actually got past parsing you ultimately don't know whether the type would then have been found in name lookup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM