这应该是一个重复的问题。但是我在谷歌上搜索了很多,并没有找到解决我的问题的命中。

我正在尝试在 osx lion 上使用 c++11 标准功能,但它不起作用。我相信这个特性被称为列表初始值设定项。根据 http://clang.llvm.org/cxx_status.html 此功能在 clang 3.1 中,这是我正在使用的版本。

下面是解释我的简单测试的 shell o/p。谁能告诉我我做错了什么?我忘记了旗帜吗?

编译器是:

:tmp$ c++ --version
Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
:tmp$ which c++
/usr/bin/c++
:tmp$ ls -alt /usr/bin/c++
lrwxr-xr-x  1 root  wheel  7 Jul 12 13:17 /usr/bin/c++ -> clang++

操作系统是
:tmp$ uname -a
Darwin .local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

测试文件是
:tmp$ cat test.cpp
#include <vector>

int main(){
  std::vector<int> vec1 {1,2,3};
  std::vector<int> vec2={1,2,3};
}

编译器输出是
:tmp$ c++ -std=c++11 test.cpp
 test.cpp:4:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec1 {1,2,3};
                   ^    ~~~~~~~
test.cpp:5:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec2={1,2,3};
                   ^    ~~~~~~~
2 errors generated.

好的,我不想到达那里,因为以下内容需要很多才能在 stackoverflow 上正确格式化。但是添加 -stdlib=c++ 会产生更多问题。现在我收到 14 个错误。
:tmp$ c++ -std=c++11 -stdlib=libc++ test.cpp
In file included from test.cpp:1:
In file included from /usr/bin/../lib/c++/v1/vector:261:
In file included from /usr/bin/../lib/c++/v1/__bit_reference:15:
In file included from /usr/bin/../lib/c++/v1/algorithm:591:
/usr/bin/../lib/c++/v1/type_traits:737:2: error: #error is_base_of not
      implemented.
#error is_base_of not implemented.
 ^
/usr/bin/../lib/c++/v1/type_traits:1700:13: error: use of undeclared identifier
      'is_base_of'
            is_base_of<_Class, typename remove_reference<_Tp>::type>::value>
            ^
/usr/bin/../lib/c++/v1/type_traits:1700:24: error: '_Class' does not refer to a
      value
            is_base_of<_Class, typename remove_reference<_Tp>::type>::value>
                   ^
/usr/bin/../lib/c++/v1/type_traits:1697:28: note: declared here
template <class _Rp, class _Class, class _Tp>
                           ^
/usr/bin/../lib/c++/v1/type_traits:1700:62: error: expected class name
            is_base_of<_Class, typename remove_reference<_Tp>::type>::value>
                                                             ^
In file included from test.cpp:1:
In file included from /usr/bin/../lib/c++/v1/vector:261:
In file included from /usr/bin/../lib/c++/v1/__bit_reference:15:
In file included from /usr/bin/../lib/c++/v1/algorithm:594:
In file included from /usr/bin/../lib/c++/v1/memory:590:
In file included from /usr/bin/../lib/c++/v1/typeinfo:61:
/usr/bin/../lib/c++/v1/exception:194:20: error: use of undeclared identifier
      'is_base_of'
                  !is_base_of<nested_exception, typename ...
               ^
/usr/bin/../lib/c++/v1/exception:194:31: error: 'nested_exception' does not
      refer to a value
                  !is_base_of<nested_exception, typename ...
                              ^
/usr/bin/../lib/c++/v1/exception:166:29: note: declared here
class _LIBCPP_EXCEPTION_ABI nested_exception
                            ^
/usr/bin/../lib/c++/v1/exception:194:81: error: parameter declarator cannot be
      qualified
  ...typename remove_reference<_Tp>::type>::value
                                   ~~^
/usr/bin/../lib/c++/v1/exception:194:85: error: expected ')'
  ...typename remove_reference<_Tp>::type>::value
                                         ^
/usr/bin/../lib/c++/v1/exception:192:18: note: to match this '('
throw_with_nested(_Tp&& __t, typename enable_if<
                 ^
/usr/bin/../lib/c++/v1/exception:213:19: error: use of undeclared identifier
      'is_base_of'
                  is_base_of<nested_exception, typename ...
              ^
/usr/bin/../lib/c++/v1/exception:213:30: error: 'nested_exception' does not
      refer to a value
                  is_base_of<nested_exception, typename ...
                         ^
/usr/bin/../lib/c++/v1/exception:166:29: note: declared here
class _LIBCPP_EXCEPTION_ABI nested_exception
                            ^
/usr/bin/../lib/c++/v1/exception:213:80: error: parameter declarator cannot be
      qualified
  ...typename remove_reference<_Tp>::type>::value
                                   ~~^
/usr/bin/../lib/c++/v1/exception:213:84: error: expected ')'
  ...typename remove_reference<_Tp>::type>::value
                                         ^
/usr/bin/../lib/c++/v1/exception:211:18: note: to match this '('
throw_with_nested(_Tp&& __t, typename enable_if<
                 ^
test.cpp:4:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec1 {1,2,3};
                   ^    ~~~~~~~
test.cpp:5:20: error: non-aggregate type 'std::vector<int>' cannot be
      initialized with an initializer list
  std::vector<int> vec2={1,2,3};
                   ^    ~~~~~~~
14 errors generated.

最佳答案

-stdlib=libc++ 添加到您的编译器标志中。有关说明,请参阅 this post

关于c++ -std=c++11 -stdlib=libc++ 在 osx lion 上给出错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14263735/

10-11 15:45