我刚刚在我的ubuntu 10.04上编译并安装了clang + llvm 3.0,以及来自svn的libc ++.由于libc ++中的状态显示线程支持已完成,我想尝试std :: async.所以我按照Anthony Williams给出的例子
只需做一些小改动就可以编译:
#include <future>
#include <iostream>
int calculate_the_answer_to_LtUaE()
{
return 42;
}
void do_stuff()
{
std::cout << "doing stuff" << std::endl;
}
int main()
{
std::future<int> the_answer=std::async(calculate_the_answer_to_LtUaE);
do_stuff();
std::cout<<"The answer to life, the universe and everything is "
<<the_answer.get()<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我编译
clang ++ --std = c ++ 0x -stdlib = libc ++ -lpthread async.cpp
但是,它运行并始终以核心转储结束:
做东西生命的答案,宇宙和一切都被中止(核心倾倒)
我检查核心转储,它显示这样的堆栈(我没有得到一个提示)
#0 0x00007fd0a1a7ba75 in raise () from /lib/libc.so.6 #1 0x00007fd0a1a7f5c0 in abort () from …
是否可以使用预处理器指令确定C++标准库是否支持C++ 11?
我目前正在开发一个使用C++ 11语言方言的项目,但是使用没有C++ 11支持的C++标准库(我需要它能够与非C++ 11库链接).
我知道我可以使用它测试C++ 11支持#if __cplusplus >= 201103L,但在我的情况下,这将评估为true.我需要了解C++ 11的C++标准库支持.
libc ++ 的std::vector 实现具有以下重载insert:
template <class _Tp, class _Allocator>
typename vector<_Tp, _Allocator>::iterator
vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
{
pointer __p = this->__begin_ + (__position - begin());
if (this->__end_ < this->__end_cap())
{
__RAII_IncreaseAnnotator __annotator(*this);
if (__p == this->__end_)
{
__alloc_traits::construct(this->__alloc(),
_VSTD::__to_raw_pointer(this->__end_), __x);
++this->__end_;
}
else
{
__move_range(__p, this->__end_, __p + 1);
const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
if (__p <= __xr && __xr < this->__end_) // [*]
++__xr;
*__p = *__xr;
}
__annotator.__done();
}
else
{
allocator_type& __a = …Run Code Online (Sandbox Code Playgroud) 众所周知(或应该)是结合的结果std::min的const参考是一个非常糟糕的主意,每当其中一个参数std::min是右值,因为const参考结合不是通过函数返回传播.所以下面的代码
#include <iostream>
#include <algorithm>
int main()
{
int n = 42;
const int& r = std::min(n - 1, n + 1); // r is dangling after this line
std::cout << r;
}
Run Code Online (Sandbox Code Playgroud)
应该产生未定义的行为,因为r悬空.事实上,当使用gcc5.2编译时使用-Wall -O3编译器吐出
警告:
<anonymous>在此函数中使用未初始化[-Wuninitialized]
但是,使用相同的标志(甚至包括)使用clang(llvm 7.0.0)进行编译时-Wextra不会发出任何警告,并且程序似乎"正常",即显示41.
问题:铿锵声使用的是"安全"版本std::min吗?就像一个版本,当一个参数是一个rvalue时,它使用一些SFINAE来按值返回?或者它是否根本不需要发出任何诊断并且程序"发生"以在此UB场景中产生"正确"结果?
我想用clang(3.3)替换gcc来构建我的C++ 11代码,所以我应该使用clang的选项-stdlib=libstdc++(让它看到STL头文件).该选项有效:clang看到类似的标题string,但找不到c ++ 11 headers(type_traits),因为clang在4.2目录中搜索:
clang++ -stdlib=libstdc++ -E -x c++ - -v < /dev/null
...
/usr/include/c++/4.2
/usr/include/c++/4.2/backward
/usr/include/clang/3.3
/usr/include
...
Run Code Online (Sandbox Code Playgroud)
如何让它看看GCC标题的永不版本?
据我所知,C++ 11的clang只支持libc ++(而不是libstdc ++),所以安装libc ++的唯一方法是什么?
我试图boost::variant使用不完整的包装类和std::vector我的间接技术来定义和访问"递归" .我的实现适用于libstdc ++,但不适用于libc ++.
这是我定义我的变体的方式:
struct my_variant_wrapper;
using my_variant_array = std::vector<my_variant_wrapper>; // <- indirection here
using my_variant = boost::variant<int, my_variant_array>;
struct my_variant_wrapper
{
my_variant _v;
template <typename... Ts>
my_variant_wrapper(Ts&&... xs) : _v(std::forward<Ts>(xs)...) { }
};
Run Code Online (Sandbox Code Playgroud)
我std::vector用来引入间接(因此动态分配将阻止my_variant具有无限大小).
由于纸张N4510 ("标准容器的最小不完全类型支持")std::vector<my_variant_wrapper>,我非常有信心我可以使用,哪里my_variant_wrapper是不完整的类型:
根据WG21的2015年页面,该论文获得批准.
根据此页面,libstdc ++始终支持这些功能.
根据这个页面,它在libc ++ 3.6中实现.
我随后访问该变体如下:
struct …Run Code Online (Sandbox Code Playgroud) 我想将c ++标准库从microsoft更改为支持c ++ 17标准的另一个.我的意思是使用libstdc ++或libc ++来交换vs lib是可能的吗?我不知道如何做到这一点.
以下代码在g ++(各种版本)上编译良好,但在我的系统上使用libc ++的clang ++ - 3.4失败:
#include <map>
#include <string>
std::map<std::string, std::string> f() {
return {};
}
int main() {
auto m = f();
}
Run Code Online (Sandbox Code Playgroud)
clang标志着以下问题:
x.cpp:6:12: error: chosen constructor is explicit in copy-initialization
return {};
^~
/usr/local/Cellar/llvm34/3.4.2/lib/llvm-3.4/bin/../include/c++/v1/map:838:14: note: constructor declared here
explicit map(const key_compare& __comp = key_compare())
^
Run Code Online (Sandbox Code Playgroud)
实际上,include文件将构造函数声明为explicit.但它在我的C++ 11草案标准中没有标记.这是clang ++/libc ++中的错误吗?我无法找到相关的错误报告.
我有一个非常简单的程序
#include <iostream>
#include <fstream>
void CHECK(std::iostream& s)
{
std::cout << "good(): " << s.good()
<< " fail(): " << s.fail()
<< " bad(): " << s.bad()
<< " eof(): " << s.eof() << std::endl;
}
int main(int argc, const char * argv[])
{
std::fstream ofs("test.txt", std::ios::out | std::ios::trunc);
std::cout << "opened" << std::endl;
CHECK(ofs);
ofs << "Hello, World!\n";
CHECK(ofs);
ofs.close();
std::cout << "closed" << std::endl;
CHECK(ofs);
ofs << "Hello, World!\n";
std::cout << "after operation" << std::endl;
CHECK(ofs);
return 0; …Run Code Online (Sandbox Code Playgroud) 我一直在尝试不同的c ++库,并发现以下内容:简单的应用程序:
#include <iostream>
int main(int argc, char* argv[])
{
try
{
throw 1;
}
catch(...)
{
std::cout << "Exception is caught\n";
}
}
Run Code Online (Sandbox Code Playgroud)
当我在ARM上编译它时:
clang++ -stdlib=stdlibc++
Run Code Online (Sandbox Code Playgroud)
异常按预期捕获.
但当我把它改为:
clang++ -stdlib=libc++
Run Code Online (Sandbox Code Playgroud)
我经常得到:
terminating with uncaught exception of type int
Aborted
Run Code Online (Sandbox Code Playgroud)
我试图用各种标志明确打开异常,如:
-fexceptions
-fcxx-exceptions
-frtti
Run Code Online (Sandbox Code Playgroud)
但这些标志都不起作用.未被捕获的例外是什么原因?可能是因为错误安装的libc ++?
PS在PC上,使用libc ++编译的相同程序按预期工作.libc ++版本在两个平台上都是相同的 - 3.7.0-1ubuntu0.1