我是C++的新手,了解C++11中可用的花括号初始值设定项。我试图创建一个简单的联盟,看起来像这个
union UExample {
UExample(const uint12_t value = 0) : raw{value} {}
uint12_t raw;
};
当我试图编译文件时,我得到了这个错误
./stack.h:18:57: error: expected member name or ';' after declaration specifiers
UBankedAddress(const uint12_t value = 0) : raw{value} {}
^
./stack.h:18:49: error: expected '('
UBankedAddress(const uint12_t value = 0) : raw{value} {}
^
./stack.h:18:55: error: expected ';' after expression
UBankedAddress(const uint12_t value = 0) : raw{value} {}
有人有办法解决这个问题吗?
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
我找到了这个问题的解决方案。它涉及MacOS上的g++
编译器。应改为使用clang++
。
之前(不工作(:
g++ stack.cpp -o stack
(工作(后:
clang++ -std=c++11 -stdlib=libc++ stack.cpp -o stack