草庐IT

c++ - 来自 std :unique_ptr to bool 的隐式转换错误

我正在使用Allegro创建一个简单的游戏。当我尝试验证指向显示器的指针不为空时,我收到编译器错误提示errorC2664:'voidvalidate(bool,std::string)':cannotconvertargument1from'std::unique_ptr>'to'bool'这是我的代码#include#include#include#includeusingnamespacestd;constintWIDTH=512;constintHEIGHT=512;voidvalidate(boolptr,stringerrorMessage){if(!ptr){cerrdi

c++ - 如何将 std::vector<uint8_t> 转换为 QByteArray?

我正在尝试从std::vector创建QByteArray。我试过了;std::vectorbuf;QByteArrayimg=newQByteArray(reinterpret_cast(buf),buf.size());但是它给出了错误;error:invalidcastfromtype'std::vector>'totype'constchar' 最佳答案 您需要转换buf.data()而不是buf:QByteArray*img=newQByteArray(reinterpret_cast(buf.data()),buf.si

c++ - std::move 的未定义行为

来自movepage的cppreferenceUnlessotherwisespecified,allstandardlibraryobjectsthathavebeenmovedfromareplacedinavalidbutunspecifiedstate.Thatis,onlythefunctionswithoutpreconditions,suchastheassignmentoperator,canbesafelyusedontheobjectafteritwasmovedfrom因此,从同一页面上的示例来看,下面的代码被认为是未定义的行为vectorv_string;str

c++ - 为什么不重新锁定互斥锁的 condition_variable 没有等待函数

考虑以下示例。std::mutexmtx;std::condition_variablecv;voidf(){{std::unique_locklock(mtx);cv.wait(lock);//1}std::coutg()“知道”f()正在等待我想讨论的场景。根据cppreference.com不需要g()在调用之前锁定互斥锁notify_one.现在在标记为“1”的行中cv将释放互斥锁并在发送通知后重新锁定它。lock的析构函数之后立即再次释放它。这似乎是多余的,特别是因为锁定是昂贵的。(我知道在某些情况下需要锁定互斥锁。但这里不是这种情况。)为什么condition_variab

c++ - 无法将派生比较传递给 std::priority_queue

我需要将派生比较器传递给std::priority_queue,但由于某种原因,正在调用基类的operator()。这是显示此行为的最小代码:classBase{public:virtualbooloperator()(intl,intr)const{cout,Base>pq((A()));pq.push(1);pq.push(2);pq.push(3);pq.push(0);coutThecodeisavailableonideoneaswell请注意,我不能使用priority_queue,A>,因为我还有其他子类Base,这将导致大量代码重复1。我做错了什么?如何将比较器传递给将

c++ - 在不区分大小写的字符串 vector 中查找字符串 C++

我有std::vectorvec;std::stringmyString;并且我需要使用不区分大小写comaprisons来确定myString是否在vec中。我知道我可以用find(vec.begin(),vec.end(),myString)!=vec.end())回答“myString是否在vec中?”这个问题但这将进行区分大小写的比较。我需要不区分大小写的比较。位置不重要,我只想知道myString是否在vec中。 最佳答案 你需要使用std::tolower和std::find_if:std::vectorvec={"AL

c++ - 使用带有匿名类型参数的 std::enable_if

我尝试将std::enable_if与未使用和未命名的类型参数一起使用,以免扭曲return类型。但是,以下代码无法编译。#includetemplate::value>>Tfoo(){std::cout::value>>Tfoo(){std::cout();foo();}编译器说:7:3:error:redefinitionof'templateTfoo()'4:3:note:'templateTfoo()'previouslydeclaredhereInfunction'intmain()':11:12:error:nomatchingfunctionforcallto'foo()

c++ - 如何格式化 std::chrono 持续时间?

有没有方便的方法将std::chrono::duration格式化为指定格式?std::chrono::high_resolution_clock::time_pointnow,then;then=std::chrono::high_resolution_clock::now();//...now=std::chrono::high_resolution_clock::now();autoduration=now-then;//baseinmicroseconds:autotimeInMicroSec=std::chrono::duration_cast(duration);如何将ti

c++ - 奇数平台上的 std::byte

阅读HerbSutter关于最近C++标准session的博客文章,它注意到std::byte已添加到C++17。作为初步阅读,我有些担心,因为它使用unsignedchar来避免严格别名规则的复杂性。我最担心的是,它如何在CHAR_BIT不是8的平台上工作?我曾在/使用过CHAR_BIT为16或32的平台(通常是DSP)。鉴于std::byte用于处理“面向字节的内存访问”,并且大多数人将byte理解为指示八位字节(而不是底层字符类型的大小),这将如何工作对于希望这将解决连续8位内存块的个人?我已经看到有人假设CHAR_BIT是8(不知道CHAR_BIT存在...)。称为std::b

c++ - 将 std::atomic_flag 包装在 getter/setter 中是否会使它的 "atomicity"无效?

假设我有一个包含std::atomic_flag作为私有(private)成员的类,通过getter公开。类似于以下内容(伪代码):classThing{private:std::atomic_flagready=ATOMIC_FLAG_INIT;public:isReady(){returnready.test_and_set();}}我天真的问题是:通过方法查询标志是否会将其变成非原子操作,成为非原子函数调用(或者是?)?我是否应该让我的ready标记为公共(public)成员并直接查询它? 最佳答案 不,它没有。test_an