草庐IT

c++ - 使用 std::array::size 实例化 std::array 时出错

示例代码test.cpp#include#includeintmain(){//OKconststd::arrayarray_int={42,1337};std::arrayarray_float_ok;//Errorconststd::arrayarray_string={"foo","bar"};std::arrayarray_float_error;return0;}使用g++4.8.4(Ubuntu14.04)编译g++-Wall-std=c++0xtest.cpp-otest给出如下错误信息test.cpp:Infunction‘intmain()’:test.cpp:14:

c++ - 在什么情况下 std::unique_ptr::operator[] 可能抛出?

我的类有一个operator[],它所做的就是在unique_ptr上调用std::unique_ptr::operator[]>成员(member)。相关部分就是这样:templatestructFoo{T&operator[](constsize_tpos)constnoexcept{returndata_[pos];}std::unique_ptrdata_;};我已将运算符标记为noexcept。但是,unique_ptr::operator[]不是noexcept。我无法找出原因,也不知道我是否可以假设它永远不会抛出。unique_ptr::operator[]本身没有在文档

c++ - gcc 的 std::bind 在哪里将参数复制到数据结构中?

为了了解std::bind分配内存的情况,我查看了thisanswer,这给出了一些直觉,但我想要更详细的了解,所以我去查看了gcc的源代码。我正在检查followingsourcecode对于来自C++标准库的gcc实现的std::bind。/***@briefFunctiontemplateforstd::bind.*@ingroupbinders*/templateinlinetypename_Bind_helper::value,_Func,_BoundArgs...>::typebind(_Func&&__f,_BoundArgs&&...__args){typedef_Bi

c++ - 关于 std::unordered_multimap 中键唯一性的保证

我想知道std::unordered_multimap中关键对象的唯一性在处理迭代时。我将尝试解释这一点:我需要将一些数据与map中的键类型相关联,这些数据不应在Hash中考虑。或KeyEqual元素,但我需要它来避免与其存储单独的map(出于优化目的)。所以与我的想法相关的代码如下:structKey{void*data;mutableboolattribute;Key(void*data):data(data),attribute(false){}booloperator==(constKey&other)const{returndata==other.data;}};struct

c++ - 了解 C++ 中放宽的内存顺序

std::atomicunique_ids;voidfoo(){inti=unique_ids.fetch_add(1,std::memory_order_relaxed);std::coutthreads;for(inti=0;i我的目标是使用atomic为并发程序生成唯一的id,但我不关心顺序。对于上面的代码,我的理解是foo中的输出值应该是不同的,尽管它们的顺序不保证。我对上面的代码进行了一百次测试,所有结果都符合我的预期。我是原子/内存顺序的初学者,谁能帮助我阐明我的理解?提前致谢。爱民附言我想指出这个问题与c++,std::atomic,whatisstd::memory_o

c++ - 我应该返回 gsl::span<const T> 而不是 const std::vector<T>&

我有一个带有std::vector成员的类和一个返回对该vector的const引用的成员函数。classdemo{public://...conststd::vector&test()const{returniv;}private:std::vectoriv;};我计划将成员类型更改为不同的数组,如具有足够功能和较小内存占用的容器类型(例如std::experimental::dynarray、std::unique_ptr)。因此,我认为最好不要将真正的容器作为const引用返回,而是将View作为gsl::span返回给元素。classdemo{public://...gsl::

c++ - 将 std::complex<double> 类型转换为 __complex128

我正在尝试使用GCC中的quadmath库。我有一个复数double值,我想将其类型转换为相应的四精度复数__complex128。以下是一个最小的(非)工作示例:#include#include#includeusingnamespacestd::complex_literals;intmain(){std::complexx=1+2i;std::printf("x=%5.5g+%5.5g\n",x.real(),x.imag());__complex128y=2+2i;y=x;return0;}当我尝试编译这段代码时g++test.cpp-lquadmath-otest我收到以下错

c++ - 命名空间 "std"没有成员 "clamp"

VS2015不会编译我的代码,说命名空间“std”没有成员“clamp”,尽管intellisense可以很好地识别它并告诉我参数和返回值。是的,我已经包含了标题。#include#include#include#include#includevoidsomefunc(){viewPos.y=std::clamp(viewPos.y,-0.95f,0.95f);} 最佳答案 您必须使用/std:c++latest开关来启用C++17对标准的添加。https://blogs.msdn.microsoft.com/vcblog/2016

c++ - 使用派生自 `std::get` 的元素在 `std::tuple` 上调用 `std::tuple` - 格式错误?

structY{};structX:std::tuple{};intmain(){std::get(std::make_tuple(X{}));}onwandbox当使用libc++时,上面的代码可以通过clang++编译并按预期工作。当使用libstdc++时,上述代码无法同时使用clang++和g++进行编译,并出现以下错误:include/c++/7.0.1/tuple:1302:36:error:nomatchingfunctionforcallto‘__get_helper(std::tuple&)’{returnstd::__get_helper(__t);}~~~~~~~

c++ - std::futures 和异常

给定以下源代码#include#include#include#include#includeintmain(){autotask=std::async(std::launch::async,[]{std::this_thread::sleep_for(std::chrono::milliseconds(1000));throwstd::runtime_error("error");});try{while(task.wait_for(std::chrono::seconds(0))!=std::future_status::ready){std::cout我希望程序会以Valid:0