草庐IT

c++ - 为什么 C++ 允许将 std::initializer_list 强制转换为基本类型,并用于初始化它们?

这个问题是关于std::initializer_list,以及为什么它被允许初始化原始类型。考虑以下两个函数:voidfoo(std::stringarg1,boolarg2=false);voidfoo(std::stringarg1,std::dequearg2,boolarg3=false);为什么会这样,当这样调用foo时:foo("somestring",{});选择了第一个重载,而不是第二个?好吧,实际上不是为什么它被选中,而是因为{}可用于初始化任何,包括原始类型。我的问题是这背后的原因。std::initializer_list采用{args...},因此在编译时不能有

c++ - std::invoke 没有匹配的重载函数发现 VS 2015 中给出的错误

我是C++和SDL的新手,我正在尝试创建一个不断更新屏幕的线程,但我不断收到以下错误:'std::invokenomatchingoverloadedfunctionfound'和'Failedtospecializefunctiontemplate'unknown-typestd::invoke(Callable&&,_Types&&...)''main.cppintmain(intargc,char**argv){usingnamespacestd::placeholders;boolgameover=false;inttest;std::stringfilepath=getRes

c++ 为什么 std::async 比顺序执行慢

#include#include#include#include#include#include#include#include#includetemplatedoubletiming(Task&&t,typenamestd::result_of::type*r=nullptr){usingnamespacestd::chrono;autobegin=Clock::now();if(r!=nullptr)*r=std::forward(t)();autoend=Clock::now();returnduration_cast>(end-begin).count();}templated

c++ - 在 C++11 中,在函数中返回一个 std::string 是移动还是复制它?

假设我有以下代码。std::stringfoo(){std::stringmystr("SOMELONGVALUE");returnmystr;}intmain(){std::stringresult=foo();}当我调用“foo”时,mystr中的数据是复制还是移动到result中?我相信它是移动的C++11风格,但我希望得到澄清和/或链接来表明这一点。谢谢!编辑:我想在使用g++为c++11或更高版本编译时我想知道这个问题的答案。 最佳答案 您的示例属于所谓的命名返回值优化,它在thisparagraphoftheC++11s

c++ - 为 std::vector<std::vector<TYPE>> 中的内部 vector 保留内存

我喜欢为std::vector>中的内部vector保留内存,为了避免在随后的push_back期间进行大量的单一内存分配。.我不知道innerSizevector的精确度,但我可以给出一个很好的估计。std::resize可以用作vecs.resize(outerSize,std::vector(innerSize));哪里outerSize和innerSize给出整数。这对我不起作用,因为默认构造函数不适用。然而std::reserve不提供这样的接口(interface)。这是为所有内部vector保留内存的好方法吗?vecs.resize(outerSize);for(auto

c++ - 更正友元定义以授予 std::map 对私有(private)默认构造函数的访问权限

我正在开发一个使用结构的库,该结构不应具有该库用户可访问的默认构造函数。structExample{Example(intx);private:Example();};在库中,std::map需要默认构造函数来创建新条目。该库非常小心地在使用默认构造函数的任何地方实际放置值。库使用映射来存储这些结构,如下所示:std::mapdata;检查HEREFORACOMPLETEEXAMPLE在ideOne中。我想阻止库的用户使用默认构造函数。我如何与std::map、std::pair和/或std::tuple交friend以允许std::map使用此默认构造函数?friendclassst

c++ - 带有 enable_if 和重载的 SFINAE

我环顾四周,但无法找到解决我的具体问题的方法。我有代码:templatetypenamestd::enable_if::value||std::is_enum::value,std::string>::typeconvertToString(constTargument){returnstd::to_string(argument);}std::stringconvertToString(std::stringstring);代码应该做什么:对任何数字类型(int、float、double和ENum)使用模板版本,对其他任何类型使用std::string版本。代码本身编译得很好,但是当

c++ - 为什么 null std::optional 被认为小于任何值,而不是更多

让nullstd::optional小于任何保持值是否有任何数学意义?或者它只是为了保持一致性而制作的,如果它是“更多”而不是“更少”就不会有任何区别? 最佳答案 直接来自theproposal(强调我的):Anumberofwaysofincludingthedisengagedstateincomparisonshavebeensuggested.Theonesproposed,havebeencraftedsuchthattheaxiomsofequivalenceandstrictweakorderingarepreserv

c++ - 为什么 std::basic_ios 会重载一元逻辑否定运算符?

C++IO流的基类std::basic_ios定义了operatorvoid*()以返回!fail()和operator!()返回fail()。这让我想知道为什么我们根本需要operator!()。当然,!is也可以通过隐式调用operatorvoid*()并取反其结果来工作。我在这里遗漏了什么,还是纯粹出于历史原因定义了std::basic_ios::operator!()?Aquestiononcomp.lang.c++.moderated也没有带来任何答案。 最佳答案 使用旧的(阅读:cfront后不久)C++编译器,编译器不

c++ - 链接器错误:对 `std::ctype<char>::_M_widen_init() 的 undefined reference

我在尝试运行示例项目时遇到链接器错误。你能告诉我如何解决这个问题吗?提前致谢。make[1]:Enteringdirectory`/home/rumi/MobiusProject/Multiproc-Paper/Transformer/ssg'/usr/bin/g++-w-DMOBIUS_LITTLE_ENDIAN-DMOBIUS_LINUX-m32-ossgGen_Linux-L../../lib/Linux_lib/-L/home/rumi/Mobius/mobius/Mobius-2.3/Cpp/lib/Linux_lib-L/home/rumi/Mobius/mobius/Mo