草庐IT

c++ - 如何使对模板函数的调用不那么冗长

有一个函数templatevoidfoo(std::functioncallback);我向其中传递回调。我想做这样的事情foo(bar);例如,bar是voidbar(inta,longb,doublec,floatd);但这给了我error:nomatchingfunctionforcalltobar(void(&)(int,longint,double,float))我必须调用foo为foo(std::function(bar));太冗长了。甚至foo(bar);会更好。foo(bar);会很理想。无论如何,我怎样才能使对foo的调用不那么冗长?编辑foo的声明必须保持不变。

C++ 将模板类型名类作为函数参数传递

我需要将模板类作为参数传递给函数,但我可以在函数内检索类型名称以初始化时间变量类声明如下:templateclassListIndex_Linked这里是类在main中的初始化和函数的调用ListIndex_LinkedL;insertion(L);以及我正在尝试做的事情template>voidinsertion(list&L){Type&temp=L.get(0);{intmax=L.numElem();for(inti=1,;i但是我得到这个错误:error:'list'isnotatemplatevoidinsertion(list&L)^在此先感谢您的帮助

c++ - 模板推导趣事,c++

考虑一下代码的和平:templatevoidf(constT&t){staticintx=0;cout我已经调用了3种类型的函数。虽然5和j可以是同一个东西,只是int,constinti肯定是不同的类型。但无论如何我的输出是:123所以这意味着编译器为不同的类型实例化相同的函数。我对么?谁能解释一下为什么? 最佳答案 来自[temp.deduct.call]:Templateargumentdeductionisdonebycomparingeachfunctiontemplateparametertype(callitP)wit

c++ - 将 make_shared 与可变参数模板绑定(bind)

我正在尝试编写以下工厂类,但找不到正确的语法:templateclassFactory{public:Factory(TArgs...args){creator_=std::bind(&std::make_shared,args...);//^^^someerroraroundhere}std::shared_ptrCreate()const{returncreator_();}private:std::function()>creator_;};这就是我使用工厂的方式:classFoo{public:Foo(boolvalue){}};classBar{public:Bar(cons

c++ - 调用仅允许子类作为参数的模板化方法

假设我有一组继承自单个父类(superclass)S的类:classS{...};classC1:publicS{...};classC2:publicS{...};然后假设我有一个模板化方法:templatevoidfoo(T*instance);我想静态检查foo从未被调用以提供父类(superclass)的实例,而仅被调用以提供(具体的)子类之一(例如显式调用foo(x))这可能吗? 最佳答案 首先,我们可以编写一个特征来检查T是否派生自S,而不是S:templateusingis_strict_base=std::integ

c++ - 防止非类型模板参数中的类型违规

我通常使用std::size_t模板参数中需要整型常量的地方。但我注意到,类型系统并没有保护我免受那些乐于将负数作为参数传递给这些参数的用户的影响。例如下面的编译给出了灾难性的结果:#includetemplatestructE1{staticvoidapply(){std::coutconstexprTa=T{-1};templatevoidE2(){for(auto&&i:{Is...})std::cout::apply();E2();//std::cout;}Demo有趣的是,变量模板不允许这样做(取消注释main中的最后一行会导致编译错误)。对于struct和function情

c++ - 标签调度、可变参数模板、通用引用和遗漏的 const 说明符

请考虑以下示例(标签分发、可变参数模板、完美转发等,全部合而为一):#include#include#includestructA{};structB{};voiddoIt(A&&,conststd::string&){std::coutvoiddoIt(T&&,Args&&...){std::coutvoidfn(Args&&...args){doIt(T{},std::forward(args)...);}intmain(){conststd::stringfoo="foo";std::stringbar="bar";fn(foo);fn(bar);fn(foo);}在这种情况下,

c++ - 限制类的模板 friend

考虑以下代码:#includeclassS{staticconstinti=42;templatefriendvoidf();};templatevoidf(){std::cout();f();}我在这里只想允许访问类的私有(private)部分S至f,但不适用于f.IE。我想得到类似'i'isaprivatememberof'S'的编译器错误对于f()行。如何实现? 最佳答案 模板实例化是一个函数,所以命名就可以了:voidf().不过,您需要事先声明:[C++03:11.4/9|C++11/C++14:11.3/11]:Ifaf

C++ 模板类语法

在我的类里面,我们正在学习C++98,所以我正在努力寻找合适的语法。声明应该怎么写:templateclassA{public:A();A(constA&rhs);A&operator=(constA&rhs);};或者应该是这样的:templateclassA{public:A();A(constA&rhs);A&operator=(constA&rhs);};我猜他们两个的实现是一样的。它们之间有什么不同吗? 最佳答案 给定templateclassA{...};名字A和A都是引用A的有效名称在类的范围内。大多数人更喜欢使用更简

c++ - 在函数模板中使用 lambda,无法推断类型,makeSet() 用例

我想要实现的是一个接受三个参数的makeSet()函数、一对迭代器和一个转换值的函数。一个用例可能是从一系列值创建一个集合并进行转换,例如,转换std::map至std::set>.客户端代码可能是这样的autos=makeSet(hash.begin(),hash.end(),[](std::pairx){returnstd::make_pair(x.second,x.first);});我目前的尝试如下,//(commentedcodearesomeother*failed*attempt).template::value_type,templateclassMonad,typen