草庐IT

std-span

全部标签

c++ - std::future::wait_for 虚假唤醒?

std::condition_variable::wait_for采用可选谓词在内部处理虚假唤醒。std::future::wait_for没有任何此类可选参数。如果我想确保等待指定的超时时间至少,或者是否已经以其他方式处理,是否需要防止虚假唤醒? 最佳答案 只有条件变量可以“虚假地”唤醒。显然,允许虚假唤醒简化了某些系统上条件变量的实现。(C++编程语言第4版。) 关于c++-std::future::wait_for虚假唤醒?,我们在StackOverflow上找到一个类似的问题:

c++ - 如何有效地将 std::vector 视为 C 缓冲区?

对于Cbuffer,我经常这样做:BYTEbuffer[MAX_SIZE];intdataSize=0;while(appRunning()){dataSize+=Receive(buffer+dataSize,MAX_SIZE-dataSize);intprocessedSize=ProcessBuffer(buffer,dataSize);ASSERT(processedSize是否可以在不损失太多性能的情况下使用std::vector这样做?编辑:我找到了一种用std::vector替换原始C缓冲区的方法。std::vectorvbuf;vbuf.reserve(MAX_SIZE

c++ - 使用 std::string 在 DLL 中创建类。 C4251 警告

我想在DLL中实现一个简单的类,例如:classMY_EXPORT_IMPORTMyClass{public://std::stringanyPublicStr;//seepoint3protected:std::stringanyStr;};问题是VisualC++编译器(在本例中为2013)抛出以下警告:C:...MyClass.hpp:X:warning:C4251:'MyClass::postfix':class'std::basic_string,std::allocator>'needstohavedll-interfacetobeusedbyclientsofstruct

c++ - 解决 std::showbase 不加前缀零的问题

无法在线找到帮助。有什么办法可以解决这个问题?std::showbase只为非零数字添加前缀(例如,0x在std::hex的情况下)(如解释here)。我想要一个格式化为0x0的输出,而不是0.但是,仅使用:std::cout不是一个选项,因为右侧参数可能并不总是整数(或等价物)。我正在寻找一个showbase替代品,它会在0前加上0x而不是扭曲非整数(或等价物),像这样:usingnamespacestd;/*Desiredresult:*/cout非常感谢。 最佳答案 尝试std::cout此方式号码将以0x为前缀总是,但你必须

c++ - std::condition_variable::wait with predicate

在std::condition_variable的文档中,有一个以谓词函数作为参数的wait()重载。该函数将等到谓词函数为真的第一个wake_up。在documentation据说这等同于:while(!pred()){wait(lock);}还有:Thisoverloadmaybeusedtoignorespuriousawakeningswhilewaitingforaspecificconditiontobecometrue.Notethatbeforeentertothismethodlockmustbeacquired,afterwait(lock)exitsitisals

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++ - 我应该返回 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::