草庐IT

template-auto

全部标签

C++ 模板部分特化 : Why cant I match the last type in variadic-template?

我尝试编写一个IsLast类型特征来检查给定类型是否是std::tuple中的最后一个类型,但下面的代码无法编译。我知道如何绕过它,但我很好奇为什么编译器不喜欢它。我想一定有一些我不知道的关于可变参数模板特化的规则。代码位于:https://godbolt.org/g/nXdodx错误信息:error:implicitinstantiationofundefinedtemplate'IsLast,int>,int>'还有关于特化声明的警告:warning:classtemplatepartialspecializationcontainstemplateparametersthatca

c++ - 使用 std::iterator traits 和 auto 在函数声明中定义一个函数

今天我尝试实现基数排序。该函数必须有两个变量:开始迭代器和结束迭代器,并且可以有第三个:一些必须返回整数类型以进行排序的函数。默认情况下,它必须是恒等函数。我的尝试看起来像(抱歉,代码看起来又长又脏,但这只是一个尝试):templatevoidradix_sort(ForwardItfirst,ForwardItlast,std::function::value_type)>get_value=[](consttypenamestd::iterator_traits::value_type&x){returnx;}){//...}get_value的返回类型当然会在编译时知道。用法应该

c++ - 警告 : specialization of template in different namespace

通过以下代码我得到了警告:warning:specializationof‘templatestructstd::iterator_traits’indifferentnamespace[-fpermissive]templateclassstd::iterator_traits{public:typedefWorddifference_type;typedefWordvalue_type;typedefToken_ptrpointer;typedefWord&reference;typedefstd::bidirectional_iterator_tagiterator_catego

c++ - 我们什么时候需要 .template 结构

我做了如下程序#include#includetemplatestructClass{templatevoiddisplay(){std::coutvoidfunc(Classk){k.display();}intmain(){Classd;func(d);}上面的程序没有编译因为display()是一个模板成员函数,所以.template的资格之前display()必须完成。我说得对吗?但是当我制作下面的程序时#include#includetemplateclassmyClass{Tdummy;/*******/public:templatevoidfunc(myClassobj)

c++ - 为什么 auto_ptr 构造不能使用 = 语法

我遇到了一个对我来说意义不大的编译器错误:#includeusingnamespacestd;auto_ptrtable=db->query("select*fromt");错误:请求从“Table*”到非标量类型“std::auto_ptr”的转换但是,以下行确实有效:auto_ptrtable(db->query("select*fromt"));构造函数的这个定义阻止它按我预期的方式工作的原因是什么?我认为初始化声明使用了构造函数。这是我的auto_ptr的构造函数(来自SGISTL):explicitauto_ptr(element_type*__p=0)throw():_M_

c++ - 将 Curiously Recurring Template Pattern (CRTP) 与其他类型参数一起使用

我尝试使用CuriouslyRecurringTemplatePattern(CRTP)并提供额外的类型参数:templateclassBase{Int*i;Float*f;};...classA:publicBase{};这可能是一个错误,更合适的父类(superclass)是Base--尽管这种参数顺序不匹配不是那么明显。如果我可以在typedef中使用名称参数的含义,这个错误会更容易看到:templateclassBase{typenameSubclass::Int_t*i;//error:invaliduseofincompletetype‘classA’typenameSub

现代C++中的decltype(auto):理解与运用

在C++14及以后的版本中,decltype(auto)成为了编译器和开发者的得力助手。它主要用于在编译时推断表达式的类型,并保证推断出的类型在上下文中是有效的。decltype(auto)比传统的decltype更强大,因为它能够处理更为复杂和动态的类型。decltype(auto)的工作原理decltype(auto)会根据初始表达式的类型进行推断,并在必要时对推断出的类型进行调整,以确保类型安全和一致性。例如,如果初始表达式是一个数组,decltype(auto)将推断出数组的元素类型;如果初始表达式是一个函数,decltype(auto)将推断出函数的返回类型。decltype(aut

c++ - 模板类中 auto 的不完整类使用

下面的代码格式是否正确?classB;templateclassA{Bdo_f()const;friendautof(Aconst&a){returna.do_f();}//#1};classB{};templateBA::do_f()const{returnB{};}intmain(){Aa;f(a);}如果我将#1​​中的auto更改为B,我会收到不完整的类型错误消息。为gcc/clang使用auto编译DemoB失败Demo 最佳答案 [dcl.fct.def.general]/2:Thetypeofaparameterort

c++ - "template"不需要关键字? [gcc/clang/Comeau 错误?]

这是测试代码templatevoidf(){Tt;t.f(0);//compilesevenwithoutthe"template"keyword,whatamImissing?}classabc{public:templatevoidf(int){}};intmain(){f();}我正在使用g++4.4.6。谢谢P.S:我已经大大编辑了我的问题。请不要介意。编辑:我向EDG的人问了这个问题,这是MikeHerrick不得不说的Wedodiagnosethisasanerrorin--strictmodeaswellasanymodethatenablesdependentnamel

c++ - G++ 编译器错误或错误代码? : "template definition of non-template"

作为大型程序的特征类的一部分,我尝试创建一个静态类变量,该变量可能具有不同的值,具体取决于实例化封闭类模板的类型。我已经简化了相关代码以生成我正在谈论的内容的简单示例:#include#include#includetemplatestructFoo;templatestructFoo::value>::type>{staticstd::stringmessage;};templatestructFoo::value>::type>{staticstd::stringmessage;};templatestd::stringFoo::message;对于GCC4.6,这会产生一个编译器