草庐IT

可变性

全部标签

c++ - std::bind 可变参数模板和自动返回类型

遵循thisquestion中的代码,我有一个带有可变参数模板函数的std::bind。如果我尝试提供带有auto返回值的函数模​​板,gcc会拒绝该程序:#includetemplateautoinv(Args...args){autobound=std::bind(&inv_impl,args...);returnbound;}intmain(){autob=inv(1,2);}编译错误为:foo.cc:Ininstantiationof‘autoinv(Args...)[withArgs={int,int}]’:foo.cc:41:30:requiredfromherefoo.c

c++ - 将 lambda 传递给可变参数 std::function 时的类型推导

我正在尝试使用用于处理它们的函数的类型信息从数组元组中检索值。但是,由于(部分?)需要为std::function的类型名使用标识结构,因此在这种情况下类型推导失败。这里有没有办法恢复扣除?#include#include#includeclasscomp_a{public:staticconstsize_tid=0;intval=0;};classcomp_b{public:staticconstsize_tid=1;intval=0;};classcomp_c{public:staticconstsize_tid=2;intval=0;};templatestructstorage

c++ - std::generate_n 的并行执行可变 lambda 生成器

当使用在其捕获中具有初始化程序的可变lambda对std::generate_n使用并行执行时,并行访问初始化值是否线程安全?[MCVE]#include#include#includeintmain(){std::vectorv(1000);std::generate_n(std::execution::par,v.data(),v.size(),[i=0]()mutable{returni++;});return0;}访问捕获的i是线程安全的吗? 最佳答案 首先我们来看一下generate_n的签名:templateForwar

c++ - boost::序列化可变成员

使用boost::serialization,序列化包含可变成员中的缓存派生值的对象的“最佳”方法是什么?classExample{public:Example(floatn):num(n),sqrt_num(-1.0){}//computeandcachesqrtonfirstreadfloatget_sqrt()const{if(sqrt_numvoidserialize(Archive&ar,unsignedintversion){...}private:floatnum;mutablefloatsqrt_num;};出于维护原因,我想避免将serialize()拆分为单独的sa

c++ - 搜索有关如何在 C++ 中实现不可变数据结构的提示

我想知道如何在C++(或C)中实现不可变数据结构。我正在寻找有关该主题的书籍或论文(或相对简单且有文档记录的实现),但我现在还没有找到,所以我决定寻求提示。预先感谢您的回答。 最佳答案 我认为您可能会从其他语言中汲取灵感。例如,在Java和C#中,不变性的实现方式如下。我们不是创建“突变器”(“改变”对象状态的函数),而是创建返回新的“已更改”实例的函数:classFoo{public:Foo(inti):i_(i){}intGetI()const{returni_;}FooSetI(inti)const{returnFoo(i);

c++ - 在位置 N 处检索 C++ 可变参数模板常量参数值的适当方法是什么?

我想知道在位置N(N在编译时已知)处检索可变参数模板常量参数值的正确方法是什么。例如,假设您有一个模板接收可变数量的函数指针作为参数,您需要检索第二个函数指针。现在,我能想到的只有这个……typedefint(*func)(int);templatestructtestme{inlineintgetme(intp)const{returnstd::array{F...}[1](p);}};...不用说,这是非常骇人听闻的。有一个更好的方法吗?谢谢。编辑:基于typedeftemplate的代码,我制作了一个可以接受任何类型作为可变模板参数的版本。它已经过测试,可以在GCC4.6的实验版

c++ - 将可变参数模板粘合到可变参数函数

为了绕过GCC在libc++中未实现的始终内联的可变参数函数,我认为我可以将可变参数函数(如snprintf,更准确地说,*_l变体)包装在可变参数模板中以实现类似的效果。实例化将填充可变参数函数的可变参数,从而使函数可以很好地内联。问题是,我对编写可变参数模板一无所知,而且我当然不知道如何将模板参数转换为单独的参数。我要替换的代码的形式是:int__sprintf_l(char*__s,locale_t__l,constchar*__format,...){va_list__va;va_start(__va,__format);int__res=vsprintf_l(__s,__l,

c++ - 将可变数量的数组引用传递给具有可变参数模板的函数

我知道如何编写接受可变数量参数的可变参数模板函数:templatevoidf(){//whatever}而且我知道如何编写接受数组引用的模板函数:templatevoidf(T(&arr)[Length]){//whatever}但我想不出如何将两者结合起来,以便函数接受可变数量的数组引用。我的第一次尝试是templateunsignedintarrlen(T(&)[Length]){returnLength;}templateintf(T(&arr)[Length]){returnLength;}templateintf(T(&arr)[Length],Rest...rest){re

c++ - 如何访问可变模板参数包成员中存在的内部模板 typedef?

我有一些代码对我来说似乎没有歧义,但gcc4.7令人窒息:#include#includeusingnamespacestd;//Containerformixinstemplateclass...Mixins>structMix:Mixins>...{typedeftuple>...>types;};//OuterlayerextractsthetypetuplefromtheargumenttemplatestructInnerCombiner{typedeftypenameInnerCombiner::typetype;};//Typedeftypetobeanewmixofth

c++ - 专门化非模板类的可变参数模板成员函数

这段代码有问题:#includeusingnamespacestd;classA{public:templatevoidstuff(Args...args);};templatevoidA::stuff(Args...args){coutvoidA::stuff(){cout();b.stuff();}Tryingtocompileit,我得到这个错误:template-id'stuff'for'voidA::stuff()'doesnotmatchanytemplatedeclaration我做错了什么?我在没有可变参数的情况下尝试过它并且它有效,但是我如何专门化可变参数模板成员函数