草庐IT

std-ranges

全部标签

c++ - 为什么 std::move 将右值引用作为参数?

根据cppreference.com,move有签名templatetypenamestd::remove_reference::type&&move(T&&t)noexcept;为什么它需要一个右值引用T&&t作为它的参数?同样当我尝试下面的代码时voidfoo(int&&bar){cout我收到编译器错误“右值引用不能绑定(bind)到左值”这是怎么回事?我很困惑。 最佳答案 这不是一个右值引用,而是一个forwardingreference;这可以保留参数的值类别。这意味着std::move可以接受左值和右值,并将它们无条件地

C++11 std::this_thread::sleep_until() 在使用 GCC 4.8.5 编译时挂起

考虑以下程序:#include#includeintmain(){std::this_thread::sleep_until(std::chrono::steady_clock::now()-std::chrono::seconds(10));return0;}当用GCC4.8.5编译时,它会挂起。用GCC4.9及以上或clang3.4及以上编译时,立即返回,为什么会挂?据我了解,GCC4.8.5完全支持C++11标准。 最佳答案 这是一个已确认的错误,已在gcc4.9中修复。https://gcc.gnu.org/bugzilla

c++ - std::function 是否比自动存储 lambda 函数重

我听说在处理lambda函数时,std::function的成本比auto高。有效的现代c++item5。我想要的是通过一些示例代码阐明为什么std::function使用的内存比auto更多的机制。有人可以帮助我吗?编辑classWidget{public:Widget(inti):i_(i){}booloperatori_;}intvalue()const{returni_;};private:inti_;intdummy_[1024];};intmain(){//performancedifferencebetweenautoandstd::function{autoless1=

c++ - std::bind 和作用域后堆栈使用

所以,今天我在运行一些使用AddressSanitizer构建的代码时,偶然发现了一个奇怪的作用域后堆栈使用错误。我有这个简化的例子:#includeclassk{public:operatorint(){return5;}};constint&n(constint&a){returna;}intmain(){kl;returnstd::bind(n,l)();}ASAN提示最后一行代码:==27575==ERROR:AddressSanitizer:stack-use-after-scopeonaddress0x7ffeab375210atpc0x000000400a01bp0x7f

c++ 何时返回 const char* 而不是 std :string

在下面的类中,speak()返回constchar*而不是std::string有什么好处或原因吗?classAnimal{protected:std::stringm_name;Animal(std::stringname):m_name(name){}public:std::stringgetName(){returnm_name;}constchar*speak(){return"???";}}; 最佳答案 std::string带有许多可能不需要和未使用的功能。如果您不想要所有这些功能,您应该考虑使用成本。传递std::st

c++ - clang 的 'range-loop-analysis' 诊断是关于什么的?

背景:考虑以下example:#include#includeintmain(){std::vectorvectorBool{false,true};for(constauto&element:vectorBool)std::cout它发出警告:test.cpp:6:21:warning:loopvariable'element'isalwaysacopybecausetherangeoftype'std::vector'doesnotreturnareference[-Wrange-loop-analysis]for(constauto&element:vectorBool)std:

c++ - 将 std::array 与传统数组 C++ 进行比较

我正在尝试对以下元素进行比较:std::vector>_targets={{0x00,0x00,0x00,0x00,0x00,0x11}{0x00,0x00,0x00,0x00,0x00,0x22}};到传统数组:uint8_t_traditional[6]={0x00,0x00,0x00,0x00,0x00,0x33}作为:for(autotarget:_targets){if(!memcmp(target,_traditional,6)){known=1;}}并且收到数据转换错误:error:cannotconvert'std::array'to'constvoid*'forarg

c++ - std::vector 的容量会减少吗?

C++14finalworkingdraft对std::vector做出以下评论:Storagemanagementishandledautomatically,thoughhintscanbegiventoimproveefficiency.cppreference说:Thestorageofthevectorishandledautomatically,beingexpandedandcontractedasneeded.和WikipediaentryforDynamicarray说:C++'sstd::vectorandRust'sstd::vec::Vecareimplemen

c++ - 是否保证在 std::string 之前初始化指向字符串文字的指针?

//file1.cppexternconstchar*foo;std::stringbar=foo;//file2.cppconstchar*foo="foo";标准保证bar被初始化为"foo"吗?或者它是否可以在foo被设置并在构造函数中出现段错误之前初始化,即SIOF的情况? 最佳答案 常量初始化保证首先发生(在这种情况下为foo)。所以Isbarguaranteedbythestandardtobeinitializedto"foo"?是的。Orcoulditbeinitializedbeforefoogetssetands

c++ - 如果 new_size 不大于旧的,C++ 标准是否保证 std::string::resize(new_size) 不会导致分配?

这个问题在这里已经有了答案:Doesthestandardguarantee,thatstd::string::resizewillnotdoreallocatememory,ifthenewsizeislessthanorequaltoastheoldone?(1个回答)关闭3年前。#include#includeintmain(){autos="hello"s;autop=&s[0];s.resize(3);assert('h'==*p);//alwaysok?}如果new_size不大于旧的,C++标准是否保证std::string::resize(new_size)不会导致分配