似乎clang(3.4)自动接受一些c ++ 11(例如auto,for(:))而没有特殊标志(虽然产生警告),但不是其他部分(例如lambdas).
例如,以下编译clang++ c++11.success.cpp:
#include <vector>
int main( int argCount, char ** argVec )
{
std::vector<int> vec;
for( auto & item : vec )
{
++item;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这失败了clang++ c++11.failure.cpp:
#include <vector>
int main( int argCount, char ** argVec )
{
std::vector<int> vec;
auto lambda = [] ( int & foo ) { return ++foo; }; //This line fails at []
for( auto & item : vec )
{
lambda( item );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
随着clang++ c++11.failure.cpp -std=c++11课程的成功为止.
我找不到任何具体文档,说明c++11没有支持哪些功能-std=c++11以及为什么支持这些功能.有人有线索吗?
Clang(与任何其他 C++ 编译器一样)具有一些语言扩展(有一个 C++11 扩展列表,可在 C++03 中使用)。这种扩展之一是基于范围的 for 循环。您可以通过 进行测试#if __has_extension(cxx_range_for) ...。无论如何它都会生成警告(如果您不使用 禁用它-Wno-c++11-extensions)。您可以使用以下方法测试这些功能:
#if __has_extension(cxx_range_for)
#warning __has_extension(cxx_range_for) is true
#else
#warning __has_extension(cxx_range_for) is false
#endif
#if __has_feature(cxx_range_for)
#warning __has_feature(cxx_range_for) is true
#else
#warning __has_feature(cxx_range_for) is false
#endif
#if __has_extension(cxx_auto_type)
#warning __has_extension(cxx_auto_type) is true
#else
#warning __has_extension(cxx_auto_type) is false
#endif
#if __has_feature(cxx_auto_type)
#warning __has_feature(cxx_auto_type) is true
#else
#warning __has_feature(cxx_auto_type) is false
#endif
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这警告说,类型推断扩展和功能已关闭,但它有效地编译了自动指针(我猜,这是因为autoas 存储类说明符的旧含义):
main.cpp:2:2: warning: __has_extension(cxx_range_for) is true [-W#warnings]
#warning __has_extension(cxx_range_for) is true
^
main.cpp:10:2: warning: __has_feature(cxx_range_for) is false [-W#warnings]
#warning __has_feature(cxx_range_for) is false
^
main.cpp:16:2: warning: __has_extension(cxx_auto_type) is false [-W#warnings]
#warning __has_extension(cxx_auto_type) is false
^
main.cpp:22:2: warning: __has_feature(cxx_auto_type) is false [-W#warnings]
#warning __has_feature(cxx_auto_type) is false
^
Run Code Online (Sandbox Code Playgroud)
为了完全符合标准,您应该通过启用 来将警告视为错误-Werror。