草庐IT

c++ - 具有默认模板参数的多态类的模板推导失败

考虑这个例子:#includetemplateclassA{};templateclassB:publicA{};templatevoidfoo(std::shared_ptr>test){}intmain(){autop=std::make_shared>();foo(p);//Worksfoo(p);//Doesnotworkfoo(p);//Doesnotwork}我试图在不明确指定foo的类型T的情况下编译它,但它不起作用。我不确定为什么就好像我显式地指定了T类型一样,它工作得很好,但如果我这样做了,它就无法编译,即使我已经告诉编译器如果我不这样做的话T类型应该是什么明确指定。

c++ - 模板推导似乎是错误的

这个问题在这里已经有了答案:Functiontemplatespecialization(3个答案)关闭9年前。推导的模板似乎是错误的,为什么调用(c)而不是(b)?#includeusingnamespacestd;templatevoidf(T){coutvoidf(int*){coutvoidf(T*){cout输出:f(T*)

c++ - 模板函数中 const 引用的类型推导

下面我有一个名为ProxyCall的模板函数,它接受一个对象、一个成员函数及其参数。它只是将调用转发给成员函数。我希望能够在不使用模板限定符的情况下调用该函数(想象一下大量带有多个参数的此类调用)。类型推导大部分都有效,但当我尝试像示例中那样传递const引用参数时,编译器(msvc和gcc4.9)会感到厌烦。#includestructWidget{voidf(conststd::string&s,boolb){}};templatevoidProxyCall(T&obj,void(T::*method)(Args...),Args&&...args){(obj.*method)(s

c++ - 如何声明和定义具有推导类型的静态成员?

我需要定义一个具有复杂(许多模板参数)类型的静态成员(不是constexpr)。因此,希望有这样的东西:structX{staticautox=makeObjectWithComplexType();};但它不是C++。所以我尝试解决它,并认为下面的代码片段可以工作,但它没有:#includestructX{staticautoabc(){returnstd::string();}staticdecltype(abc())x;};decltype(abc())X::x;intmain(){}失败并出现错误:error:useof‘staticautoX::abc()’beforeded

c++ - 从 C++ 模板中的非 const 左值引用推导出 const 左值引用

假设您有以下一对函数:voidf(constint&){//Dosomething,makingacopyoftheargument.}voidf(int&&){//Dothesamething,butmovingtheargument.}它们相当冗余——函数之间的唯一区别在于它们是复制还是移动参数。当然,我们可以通过将其重写为单个模板函数来做得更好:templatevoidg(T&&){//Dosomething,possiblyusingstd::forwardtocopyormovetheargument.}这行得通,并且是实践中常用的习惯用法。但是模板可能会被实例化为三个函数,

c++ - decltype(auto) 函数返回类型不推导 && 类型

如果一个函数返回decltype(auto),它返回一个int&&类型的局部变量,为什么返回类型是int&?如果我将变量转换为它自己的类型,那么返回类型就是我所期望的(int&&)#includenamespace{autoi=5;autoj=5;decltype(auto)foo1(){int&&ret=std::move(i);returnret;}decltype(auto)foo2(){int&&ret=std::move(j);returnstatic_cast(ret);}}intmain(){static_assert(std::is_same_v);static_ass

c++ - 模板推导在 GCC 4.6 和 4.7 上的两个不同结果

考虑以下代码:#include#include#include#include//VersionAtemplatevoidf(constT&x){std::coutclassT>voidf(constT&x){std::coutclassT,TN...N>voidf(constT&x){std::cout());f(std::array());return0;}Windows上的GCC4.6.2提供:VersionAVersionBVersionCLinux上的GCC4.7.1提供:VersionAVersionBVersionA所以问题是:为什么?这是错误还是未定义的行为?我应该将其

c++ - 推导嵌套模板的模板参数失败

好吧,我通读了很多“无法推断模板参数”的问题,但似乎没有一个符合我的情况——或者我不明白答案……有one我觉得方向是正确的,但我未能为我的问题提取解决方案。我的标题中的精简代码如下所示:templateclassTemplateProblem{public://DoIreallyneedthisordidImisssomethingfromtheSTL?templatestructUnaryFunction:publicstd::unary_function{virtualToutoperator()(Tininput)=0;};templatestructStaticCast:pub

c++ - 类方法的返回类型推导? C++1y

C++14中的成员函数是否允许返回类型推导,还是仅适用于自由函数?我问是因为我隐含地假设它会起作用,但在gcc4.8.1中我得到一个内部编译器错误(“ingen_type_die_with_usage”)。我第一次遇到这样一个神秘的错误,所以我有点怀疑;我知道他们从那时起就更改了规范。为了清楚起见,这对我有用:autofoo(){return5;}但这不是:classBar{autobaz(){return5;}}标准草案允许这样做吗? 最佳答案 是的,根据论文n3582,标准应该允许这样做.这是论文中的一个例子。Allowingn

c++ - 函数模板推导和initlializer_list

我有以下函数模板:templatevoidf(std::initializer_list>il){//...}我调用函数如下:f({std::pair(1,2),std::pair(3,4)});//(a)而且效果很好。但是,如果我尝试按如下方式调用它:f({{1,2},{3,4}});//(b)它无法推断出正确的类型,我得到了一个编译错误:'nomatchingfunctionforcallto'f()notecandidateis:note:templatevoidf(std::initializer_list>)'如果我这样调用它:f({std::pair(1,2),{3,4}}