草庐IT

可变性

全部标签

c++ - 如何检查传递给可变参数函数的参数类型

我是可变参数模板的新手,为了学习考虑以下函数templateT*make_arr(args...arg){//CodetocheckifpassedargsareofthesametypeT*arr=newT[sizeof...(arg)]{arg...};returnarr;}我有两个问题:我希望函数是模板化的,我希望传递的参数是同一类型,所以问题是:是否可以检查传递的参数是否是同一类型?是否可以通过推断args...的类型来推断数组指针的类型?,我的意思是不使用?...我使用了decltype(arg)但它没有用...注意:如果不合适请编辑标题问题...谢谢

c++ - 对不带参数的可变参数模板函数的调用不明确?

运行时:templatestructCodeByType{staticconstint32_tValue=7;};templatestructCodeByType{staticconstint32_tValue=1;};templateint32_tSum(){//ThecompilercomplainsonthislinereturnSum()+Sum();}templateint32_tSum(){returnCodeByType::Value;}intmain(){autosum=Sum();}我得到:ErrorC2668'Sum':ambiguouscalltooverload

c++ - 具有递归继承和使用声明的可变参数模板

我想做一些与下面的代码类似的事情,除了我不想执行func()两次,因为它将是相同的实现。您对如何实现这一点有什么建议吗?templateclassBase:publicBase{public:usingBase::func;voidfunc(Firstobject){//implementation}};templateclassBase{public:voidfunc(Firstobject){//implementation}};structX{};structY{};structZ{};classDerived:publicBase{};//...Derivedd;d.func(

c++ - 将可变参数模板类型转换为 void,预期为 ')' 之前

在我的代码中,我使用可变参数模板函数作为日志记录机制。如果定义了DEBUG宏,则会打印一条消息;如果DEBUG未定义,那么它应该不打印任何内容。我的代码:#ifdefDEBUGinlinevoidLOG_CHAT(){}templatevoidLOG_CHAT(First&&first,Rest&&...rest){std::cout(first);LOG_CHAT(std::forward(rest)...);}#elsetemplatevoidLOG_CHAT(Rest&&...rest){(void)(rest...);}#endif我会将函数定义留空,但我不想得到“未使用的参数

c++ - 从可变参数函数将函数应用于参数包的所有元素

这个问题在这里已经有了答案:variadictemplateparameterpackexpandingforfunctioncalls(2个答案)关闭4年前。考虑以下(不起作用!)示例:#includetemplatevoidprint(consttype&item){std::coutvoidprintall(consttypes&...items){print(items)...;}intmain(){printall(1,2,"hello");}这里我有一个函数print可以简单地打印出它的参数,还有一个可变参数函数printall可以接受一组参数。现在,我想做的是简单地让pr

c++ - 可变大小的对象可能无法初始化尽管初始化错误

intmain(){intsample_rate=50;intt_max=60*5;intdimensions=3;intnum_samples=sample_rate*t_max;doubledata[dimensions][num_samples]={{}};//Errorherereturn0;}我知道堆上数组的大小必须在编译时知道,它在这里(3x15000)。为什么我仍然收到错误? 最佳答案 只需使用std::vector即可。#includeintmain(){intsample_rate=50;intt_max=60*5

c++ - 带约束的可变参数模板的 'requires' 表达式的语法是什么?

如果我有一个可变参数模板;templateconceptFooable=requires(Tt){t.bar()->bool;};structFoo{intbig_foo;templateexplicitFoo(T&&i,U&&...f)noexcept:big_foo{std::forward(i)}{Something::something(std::forward(f)...);...}};然后模板的定义及其约束按预期工作。但是如果我“要求”对Foo有更多限制,那么使用“要求”表达式格式,例如;templaterequiresstd::Integral&&Fooable&&Bil

c++ - 使用可变数量的参数调用 QMetaObject::invokeMethod()

我目前正在移植FitNesse的Slim服务器,但我现在有点卡住了。我得到的是像这样的字符串:("id_4","call","id","setNumerator","20")("id_5","call","id","setSomethingElse","10","8")其中“setNumerator”和“setSomethingElse”是应该调用的方法的名称,“20”、“10”和“8”是我传递的参数。所以我现在的问题是,我不知道如何为这两种方法使用one调用invokeMethod。我当前的解决方法如下所示://(ifinstructionLength==5)metaObj->inv

c++ - 在 C++ 中创建可变数量的 vector

我可以创建一个大小为n*m的二维数组通过做:vector>foo(n,vector(m)).假设在运行时我得到了可变数量的值,例如v_1,v_2,v_3,...,v_k并希望创建以下内容:vector...>>foo(v_1,vector(v_2,vector..));换句话说,创建一个大小为v_1*v_2*v_3...*v_k的多维数组.我怎样才能做到这一点?这可能吗? 最佳答案 你不能这样做——数据类型必须在编译时设置。也就是说,使用具有正确元素总数的单个数组并创建一个映射以便您的逻辑[i1][i2][...]是非常实用的。在说

c++ - 使用 std::enable_if 和可变参数基类

这是我正在尝试做的一个简化示例:#include#include#includetemplateclassfoo{public:templatetypenamestd::enable_if::value>::typebar(constU&t){std::coutclassbaz:publicfoo...{};intmain(){bazb;b.bar(1.0);}这给了我模棱两可的函数错误:error:requestformember'bar'isambiguousb.bar(1.0);note:candidatesare:templatetypenamestd::enable_if::