草庐IT

function-templates

全部标签

c++ - reference_wrapper : make_pair VS Class Template Argument Deduction (CTAD)

为什么make_pair和类模板参数推导(CTAD)不同意生成哪种类型?#include#include#include#includeintmain(){intmyInt=5;std::reference_wrappermyIntRef=myInt;automyPair=std::make_pair(myInt,myIntRef);std::pairMy2ndPair(myInt,myIntRef);std::cout输出:St4pairIiRiE//std::pairSt4pairIiSt17reference_wrapperIiEE//std::pair>更新:为什么std::p

采用依赖于模板参数的 std::function 的 C++11 模板函数

我正在尝试编写一个接受依赖于模板参数的std::function的模板函数。不幸的是,编译器无法正确推导出std::function的参数。这里有一些简单的示例代码:#include#includeusingnamespacestd;voidDoSomething(unsignedident,unsignedparam){coutvoidCallFunc(Identident,Paramparam,std::functionop){op(ident,param);}intmain(){unsignedid(1);unsignedparam(1);//Thefollowingfailst

c++ - 嵌套模板特化结果为 "Illegal use of explicit template arguments"?

如何专门化嵌套模板?(请参阅下面的错误。)usingstd::reverse_iterator;templatereverse_iteratormake_reverse_iterator(constIt&it){returnreverse_iterator(it);}templateItmake_reverse_iterator>(constreverse_iterator&it){//Above^//errorC2768://'make_reverse_iterator':illegaluseofexplicittemplateargumentsreturnit.base();}

c++ - 未定义对 'function' 的引用——链接器问题?

我目前正在尝试在Linux终端上编译和链接我的C++文件。我运行的命令是:g++-ogameplaygamePlay.cppplayer.cppmain.cppdisplay.cpp-lcurses该命令似乎可以完美地编译所有内容,但是一旦它尝试链接内容,我就会遇到2个错误。undefinedreferenceto'gamePlay::deal(std::vector>,std::vector>)'undefinedreferenceto'gamePlay::score(player)'下面是我的gamePlay.CPP文件。我真的迷路了,非常感谢任何帮助!#include"gameP

c++ - std::reverse_copy "error: function call has aggregate value"

#include#include#include#includeusingnamespacestd;intmain(){intarrA[]={1,2,3,4,5,6,7,8,9};vectorvecIntA(arrA,arrA+sizeof(arrA)/sizeof(arrA[0]));vectorvecIntB(vecIntA.size());//copy((vecIntA.rbegin()+3).base(),(vecIntA.rbegin()+1).base(),vecIntB.begin());//OKvector::iterators=(vecIntA.rbegin()+3)

c++ - 检查 C++11 中返回的 std::function 是否为 "valid"

我想像这样实现一个动态任务队列:typedefstd::functionJob;typedefstd::functionJobGenerator;//..JobGeneratorgen=...;autojob=gen();while(IsValidFunction(job)){job();}如何实现IsValidFunction?std::function是否有某种默认值可供检查? 最佳答案 您可以简单地检查job作为一个bool值:while(autojob=gen()){job();}这是一种简写形式,它赋值job来自gen()

c++ - 将函数分配给 std::function 类型时调用哪个函数原型(prototype)(以及如何调用)?

我有密码voidprints_one(){coutfoo;foo=prints_one;foo();return0;}它按预期工作;它打印“一个”。我不知道在赋值中调用了哪个赋值运算符原型(prototype)以及如何调用。看cppreference,好像就是这个函数templatefunction&operator=(Fn&&fn);但如果这是被调用的原型(prototype),我不明白函数如何绑定(bind)到右值引用。谢谢!更新:谢谢大家,我会阅读通用引用资料。关于40two的回答;此代码打印它是一个右值引用:templateclassFoo{public:Foo(){}Foo&

c++ - 虚拟重载与 `std::function` 成员?

我现在有一个类,我们称之为Generic.这个类有成员和属性,我打算在std::vector中使用它或类似的,处理这个类的几个实例。另外,我想特化这个类,通用对象和特化对象之间的唯一区别是一个私有(private)方法,它不访问类的任何成员(但被其他方法调用)。我的第一个想法是简单地声明它virtual并像这样在专门的类中重载它:classGeneric{//allothermembersandattributesprivate:virtualfloatspecialFunc(floatx)const=0;};classSpecialized_one:publicGeneric{pri

c++ - 在每个源文件中替代 "extern template"

我正在开发一个库,其中我们的许多核心对象都是模板,其中一个特定实例以指向该模板实例的智能指针的形式出现在项目的大多数文件中。我在单个源文件中明确实例化了这些模板。我们最近切换到C++11,我正在尝试使用新的externtemplateclassMyTemplate;加快编译速度。我的第一个问题是我是否在周围使用智能指针MyTemplate正在隐式实例化模板并要求文件顶部的“外部模板..”以避免重复实例化。我的第二个问题是是否有一些替代方法来添加所有这些externtemplateclassMyTemplate;到每个源文件。为我定义的每个模板搜索智能指针的每个实例并确保我在该文件中有正

C++ : Check if the template type is one of the variadic template types

这个问题在这里已经有了答案:Checkifatypeispassedinvariadictemplateparameterpack(3个答案)关闭7年前。假设我们有函数:templatevoidfoo(){...};检查“Kind”类型是否是C++(包括C++1z)中的“Kinds”类型之一的最简单方法是什么?