草庐IT

non-fast-forward

全部标签

c++ - 在新代码中,为什么要使用 `int` 而不是 `int_fast16_t` 或 `int_fast32_t` 作为计数变量?

如果您需要一个计数变量,那么您的整数肯定必须有一个上限和下限。那么为什么不通过选择适当的(u)int_fastxx_t数据类型来指定这些限制呢? 最佳答案 最简单的原因是人们更习惯于int,而不是C++11中引入的附加类型,并且它是语言的“默认”整数类型(C++有一个);该标准在[basic.fundamental/2]中规定:Plainintshavethenaturalsizesuggestedbythearchitectureoftheexecutionenvironment46;theothersignedintegerty

c++ - 为什么要为 std::forward_list 拼接整个列表或线性范围?

将范围从一个列表拼接到另一个列表可以在常数时间内完成,但代价是使size()的复杂度呈线性。C++11改变了std::list的情况,要求size()为常数时间。例如,这破坏了gcc的实现,参见[C++0x]std::list::sizecomplexity.除了splice()范围外,还有什么其他原因size()不能成为常量时间在早期,C++03符合std::listimplementations?为什么拼接整个列表或范围是线性的std::forward_list?参见splice_after(),案例(1)和(3)。另见standarddraftN3485中的23.3.4.6for

c++ - 使用 std::forward 而不是 std::move 来初始化对象有什么好处吗?

我有一个函数模板,它采用某种可调用类型的参数,并使用std::bind为原始可调用对象创建一个具有预定义参数值的新可调用对象。我用转发引用参数和std::forward编写了它,如下所示:templateautomake_example_caller(F&&f){returnstd::bind(std::forward(f),123,456,789);}cppreferencedocumentationforstd::bind表示绑定(bind)对象“包含一个由std::decay::type构造的std::forward(f)类型的成员对象”。由于std::bind将函数转发给它的内

c++ - `invalid initialization of non-const reference` 是什么意思?

编译此代码时,我得到以下error:Infunction'intmain()':Line11:error:invalidinitializationofnon-constreferenceoftype'Main&'fromatemporaryoftype'Main'这是我的代码:templatestructMain{staticMaintempFunction(){returnMain();}};intmain(){Main&mainReference=Main::tempFunction();//我不明白为什么?谁能解释一下? 最佳答案

c++ - std::forward_list 成员可以实现为静态的吗?

std::forward_list提供了insert_after和erase_after成员,它们可能不需要实际访问std::forward_list对象。因此,它们可以作为static成员函数实现,并且可以在没有列表对象的情况下被调用——对于想要从列表中删除自身的对象很有用,这是一种非常常见的用法。编辑:此优化仅适用于std::allocator或用户定义的无状态分配器的forward_list特化。符合标准的实现可以做到这一点吗?§17.6.5.5/3说AcalltoamemberfunctionsignaturedescribedintheC++standardlibrarybe

c++ - 我可以在类定义中放置 "non-static blocks"代码吗?

C++中有非静态block吗?如果不是,如何优雅地模拟?我想替换像这样的东西:-classC{public:voidini(){/*somecode*/}};classD{std::vectorregis;//willini();laterpublic:Cfield1;public:Cfield2;public:Cfield3;//wheneverIaddanewfield,Ihaveto...#1public:D(){regis.push_back(&field1);regis.push_back(&field2);regis.push_back(&field3);//#1...al

c++ - 为什么 `const int ci = 2; std::forward<int>(ci);` 不起作用以及如何修复/解决它?

简单的问题,为什么不thefollowing工作(意味着ci的拷贝)?#includeintmain(){constintci=2;std::forward(ci);}prog.cpp:Infunction'intmain()':prog.cpp:6:23:error:nomatchingfunctionforcallto'forward(constint&)'问题在编写一些模板内容时表现出来,我有一个简单的holder类型,如下所示。为了避免不必要的拷贝,我尽可能使用完美转发,但事实证明这似乎是问题的根源。templatestructholder{Tvalue;holder(T&&v

c++ - constexpr 类的设计 : merging constexpr and non-constexpr versions?

考虑一个在运行时只包装一个值的类:templateclassNonConstValue{public:NonConstValue(constType&val):_value(val){;}Typeget()const{return_value;}voidset(constType&val)const{_value=val;}protected:Type_value;};以及它的constexpr版本:templateclassConstValue{public:constexprConstValue(constType&val):_value(val){;}constexprTypeg

c++ - g++ 6.1 可能的 std::forward 回归 - 错误或预期行为?

g++6.1最近被引入到ArchLinux的测试库中,我的一些使用g++5.3.0成功编译的代码不再编译。我做了一个最小的例子:gcc.godbolt.orglink//Thiscodecompileswithg++5.3.0//Thisdoesnotcompilewithg++6.1#include#include#include#defineFWD(...)::std::forward(__VA_ARGS__)structsinker{templatevoidsink(T&){}};templatevoidcaller(T&v,TF&&f){sinkers;f(s,v);}temp

c++ - xvalues : differences between non class types and class types

考虑下面的最小示例:#includestructS{};intmain(){Ss;std::move(s)=S{};}它编译没有错误。如果我改为使用非类类型,则会收到错误。例如,以下代码无法编译:#includeintmain(){inti;std::move(i)=42;}枚举、作用域枚举等也是如此。错误(来自GCC)是:usingxvalue(rvaluereference)aslvalue这背后的原理是什么?我想这是对的,但我想了解我可以对除非类之外的所有类型执行此操作的原因是什么。 最佳答案 C++允许对类对象右值进行赋值,