草庐IT

c++ - 模板函数作为模板参数

我正在学习模板并尝试实现此方法:templatevoidflipArgs(Func*function,Left&&leftArg,Right&&rightArg){function(std::forward(rightArg),std::forward(leftArg));}它接受一个函数和两个参数,并使用翻转的两个参数调用给定的函数。它适用于以下功能:voidtest1(std::string,int){}当我尝试这个功能时:templatevoidtest2(T&&a,int){}与:strings("test");flip(test2,42,s);编译器(g++4.7.1)告诉我

c++ - 从其他容器中辨别 smart_pointer 的模板函数

考虑以下模板函数:templateconstT*DoSomething(constT&t){auto&id=typeid(T);coutT*DoSomething(T*t){auto&id=typeid(T);coutclasscontainer>T*DoSomething(constcontainer&t){auto&type_id=typeid(T);auto&container_id=typeid(container);coutclasscontainer,templateclassdeleter=default_delete>T*DoSomething(constcontain

c++ - 将可变参数模板参数转发给多个类成员

以下安全吗?不会std::string是moved在第一类成员初始化之后?打印出来没问题,但我不确定。templateclassTest{public:templateTest(Args&&...args):m_one(newT(std::forward(args)...)),m_two(newT(std::forward(args)...))//m_one;std::unique_ptrm_two;};classC{public:C(inta,intb,conststd::string&c):m_a(a),m_b(b),m_c(c){std::coutt(1,2,"3");}我想这没

c++ - 基于模板参数是否为 shared_ptr 的函数特化

我正在尝试检测类型是否为shared_ptr如果是,则分派(dispatch)到特定函数模板或覆盖。这是我实际尝试的简化版本:#include#include#includetemplatestructis_shared_ptr:std::false_type{};templatestructis_shared_ptr>:std::true_type{};classFoo{};typedefstd::shared_ptrSharedFoo;templatevoidgetValue();template::value>::type=0>voidgetValue(){printf("sha

c++ - 如何分别针对整型和浮点型专门化模板函数?

考虑函数templatevoidFun(Tt);.我怎样才能分别对整型和浮点型有不同的实现?我猜积木是std::enable_if,std::is_integral,std::is_floating_point.但我不能以一种优雅的方式将它们放在一起:-(.附言我有可用的C++11。 最佳答案 参见std::enable_if的示例代码在cppreference.com.编辑:将上面链接中的代码改编如下:#include#includetemplatetypenamestd::enable_if::value>::typefoo(T

c++ - Const 接收一个 var,我不能将它传递给模板

我想做的是:intconstbitsPerInt=log2(X);bitsetbits(arandomnumber...);但是我得到这个错误:'bitsPerInt'cannotappearinaconstantexpressionerror:templateargument1isinvalid 最佳答案 如果你真的需要它工作,制作你自己的在编译时工作的log2并将它传递给bitset的模板参数。constexprunsignedLog2(unsignedn,unsignedp=0){return(nbits;Liveexampl

c++ - 默认模板参数如何工作?

我知道关于模板默认参数语法的问题已经被问了很多。通常,答案(与我对它应该如何工作的理解同步)是使用类似的东西:templateclassT1{};最近我想检查Boost在其mapped_vector中使用了哪个map实现.并找到以下片段:templateclassmapped_vector:显然,参数A没有默认绑定(bind),而且显然,我可以实例化一个mapped_vector正好。显然,默认参数是以某种方式推断出来的,但是如何推断出来的呢?编辑:准确地说,我说的是thisfile中的第279行 最佳答案 行here(@Xeo的链

c++ - 自动检测不带参数的模板参数

这是ananswer的一个分支到另一个SOpost.我有以下工作代码,具有预期的输出。#includetemplateTtwice(Tin){return2*in;}structFoo{Foooperator+(int(*func)(intin))const{Fooret{data};ret.data+=func(ret.data);returnret;}intdata;};intmain(){Foof1{20};Foof2=f1+twice;Foof3=f1+twice;std::cout直到昨天我才知道,即使没有参数,编译器也可以推断出函数模板的类型参数。在上面的代码中,表达式f1

c++ - 如何在不重复代码而仅更改解析函数的情况下模板化函数?

我有一个现有的函数,可以将逗号分隔的数字字符串转换为vector,例如“1,2,3”变为[1,2,3]函数看起来非常粗略:boolConvertStringToNumberList(stringinput,vector&output){int32_tvalue=strtol(str,0,/*base*/10);}我想将其更改为适用于int32_t、uint32_t、double和float的模板函数。问题在于,对于每种数据类型,都有不同的解析函数(例如strtol、strtoul、strtod、strtof)可能采用不同数量的参数(例如strtod()不采用“基本”参数).如何在不重复

c++ - 如何编写在模板类中返回指向结构对象的指针中声明的函数的定义?

我有这样的代码:templateclassFoo{structSome_struct{Tobject;Some_struct*next;};public:Some_struct*function();//declarationofmyfunction};templateSome_struct*Foo::function()//thisdefinitioniswrong{//somethinginsidereturnpointer_to_Some_struct;}正确的定义应该是什么样的? 最佳答案 您忘记为返回类型添加适当的范围。这