草庐IT

公式推导

全部标签

c++ - 为什么在模板推导过程中没有选择正确的功能

我定义了两个重载模板函数。它们都以函数指针作为参数。不同之处在于,在第一个中,函数是类的成员,而在第二个中则不是。当我尝试传入非成员函数类的模板函数时,编译器会选择该函数是类成员的模板函数。结果产生了编译器错误。下面是代码#include#include#include#include#includeclassWorker{public:Worker(){}templatevoidQueueFunction(_Callable__f,Objectobj,_Args...__args){funcs.emplace_back([=]()mutable{(obj.*__f)(__args..

c++ - 返回类型推导

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:OmitreturntypeinC++11在C++11中,如果主体仅包含一个return语句,lambda可以推断出它们的返回类型。proposalistheworks删除这个限制,显然它已经在GCC中工作了。有什么原因不能扩展到所有auto返回函数吗?是否已提出此扩展?

c++ - 一般产品中的特征自动类型推导

我有以下一段代码(我为稍大的代码道歉片段,这是我能够将我的问题减少到的最小示例):#include#include#include#include//DynamicMatrixoverScalarfieldtemplateusingDynMat=Eigen::Matrix;//DynamiccolumnvectoroverScalarfieldtemplateusingDynVect=Eigen::Matrix;//ReturnstheDxDIdentitymatrixoverthefieldDerived::Scalar//deducedfromtheexpressionEigen:

c++ - 当参数是函数参数包时,在部分排序期间推导模板参数

N452714.8.2.4[temp.deduct.partial]3Thetypesusedtodeterminetheorderingdependonthecontextinwhichthepartialorderingisdone:(3.1)—Inthecontextofafunctioncall,thetypesusedarethosefunctionparametertypesforwhichthefunctioncallhasarguments.(3.2)—Inthecontextofacalltoaconversionfunction,thereturntypesofth

c++ - 参数的重载函数(不可)在编译时推导

有没有一种方法可以通过重载函数来区分参数是在编译时可评估还是仅在运行时可评估?假设我有以下功能:std::stringlookup(intx){returntable::value;}这允许我在恒定时间内(有空间开销)基于参数x选择一个字符串值。然而,在某些情况下x无法在编译时提供,我需要运行一个foo版本,它以更高的时间复杂度进行查找。我当然可以使用不同名称的函数,但我希望有一个统一的界面。我接受了一个答案,但我仍然对完全相同的函数调用是否可以进行这种区分感兴趣。 最佳答案 我相信你能得到的最接近的是重载lookup在int和st

c++ - 使用 constexpr 函数时推导失败?

所以我正在根据thisquestion做进一步的测试,我仍然不太清楚类型推导的工作原理。如果我使用以下内容:template::value,int>=0>inlineautofnx(T)->int{return0;}template::value,int>=0>inlineautofnx(T)->int{return0;}inlinevoidfn(){fnx(1);fnx(1.f);}我没有得到任何编译错误。但是当我这样做时:templateconstexprboolvalue(){returnTRUTH;}template::value>(),int>=0>inlineautofnx

c++ - 当类是另一个模板的模板参数时,可以使用类模板参数推导吗?

我试过这样写代码:#include#include#includeusingnamespacestd::string_literals;intmain(){std::vectorv{1,2,3};std::pairp{1,"a"s};//std::vectorvp{{1,"a"s},{2,"b"s},{3,"c"s}};//ERROR}但是它在pair是vector的模板参数的行中给出了编译错误。有没有办法让类模板推导在这里工作,或者我是否需要为对指定模板参数? 最佳答案 目前不支持。来自[dcl.type.class.deduct

c++ - 模板类型推导失败(std::empty 作为谓词)

我有一个vector的vector,我想检查它们是否都是空的。使用标准库,我试过:#include#includeintmain(){std::vector>vv;std::all_of(std::begin(vv),std::end(vv),std::empty);}这会导致clang7.0出现以下错误:/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_algo.h:508:5:note:candidatetemplateignored:couldn'tinfertemp

c++ - 将 lambda 传递给可变参数 std::function 时的类型推导

我正在尝试使用用于处理它们的函数的类型信息从数组元组中检索值。但是,由于(部分?)需要为std::function的类型名使用标识结构,因此在这种情况下类型推导失败。这里有没有办法恢复扣除?#include#include#includeclasscomp_a{public:staticconstsize_tid=0;intval=0;};classcomp_b{public:staticconstsize_tid=1;intval=0;};classcomp_c{public:staticconstsize_tid=2;intval=0;};templatestructstorage

c++ - 函数重载和模板推导优先级

考虑以下函数声明:templatef(constT&x);//Version1templatef(constMyClass&x);//Version2如果我用一个与MyClass无关的类型调用f,将调用第一个版本。如果我用MyClass类型调用f(无论模板参数类型是什么),那么将调用第二个版本。但是现在,考虑一下:templateMyDerivedClass:publicMyClass{};将为MyDerivedClass类型调用什么版本的函数? 最佳答案 这在标准的第13.3节中进行了处理。第13.3/1段指出:Eachofthe