MAC上的叮当声不支持统一初始化

Does clang on Mac not support uniform initialization?

本文关键字:初始化 不支持 叮当声 MAC      更新时间:2023-10-16

mac上的clang不支持统一的初始化?

我尝试编译以下代码,但编译器引起了错误。

#include <iostream>
#include <vector>
#include <string>
int main() {
    std::vector<int> v = {3, 1, 9, 4};
    std::cout << v[1] << std::endl;
}

错误:

vector.cpp:9:22: error: non-aggregate type 'std::vector<int>' cannot be initialized with an initializer list
    std::vector<int> v = {3, 1, 9, 4};
                     ^   ~~~~~~~~~~~~
1 error generated.

OS:MACOS 10.12.4

编译器版本:

Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

我怀疑您是不是将代码编译为C 11/14(-std=c++11-std=c++14(?如果不;去做。clang 支持您要做的事情。