草庐IT

Templates

全部标签

c++ - float 据在 istream 输入上意外成功转换为整数

我正在编写一个模板函数,该函数将检查用户是否分配了答案应包含的正确数据类型。例如:intmain(){intE;cout(cin);cout函数clear_and_read定义为:templateTclear_and_read(istream&inputstream){cin.sync();//removesanythingstillincinstreamcin.clear();Tinput;inputstream>>input;while(inputstream.fail()){cout>input;}returninput;}现在,如果我尝试输入string而不是integer,这

模板内的 C++ 初始值设定项列表

我想更多地了解C++11,所以我编写了我所知道的最简单的initializer_list测试,但在模板内部,这是一个“免费”函数,每当我编译它时我都会收到这个错误(用于漂亮格式的clang++)/home/alex/repo/mine/mlcppl/test/utiltest1.cc:16:3:error:nomatchingfunctionforcallto'makevec'mlcppl::makevec({"alex","herrmann"});代码如下:namespacemlcppl{templatevectormakevec(initializer_listinitlist){

c++ - 错误 : ‘Class’ was not declared in this scope

请问,如何在另一个类中定义类。在下面的代码中。我尝试以#define"CCompField.h"的方式定义它,但它不起作用。:(。我认为这是非常常见的编程问题,可能在互联网上已经解决了100000次,但我不知道如何找到它。感谢您的帮助。#ifndefCNEWGAME_H#defineCNEWGAME_HclassCNewGame{public:CNewGame();~CNewGame();voidBeginnerGame();voidIntermediateGame();voidAdviceGame();voidHowToPlay();voidNetGame(intmode);intM

c++ - 表达式模板 - 无法专门化函数模板

我试图制作一些表达式模板作为对thisquestion的回答,但我遇到了编译器错误,我无法弄清楚。到目前为止,我已经获得了相当小的SSCCEtemplateclassinherit2:privatesub_expr{//line3public:inherit2(sub_exprrhs):sub_expr(rhs){}templateautooperator()(constT&v)const->decltype(sub_expr::operator()(v))//line7{returnsub_expr::operator()(v);}};classexpression_paramete

c++ - 模板隐式实例化和内联成员

我想知道什么时候调用模板类的成员函数。在哪里生成定义?例如:templateclassA{public:A(){cout::A()"::f()"ob;//Timet1ob.f();//Timet2}所以我想知道模板类A是做什么的看起来像point1&point2案例1:时间t1:classA{public:A(){cout::A()"::fyetsothereisjustadeclaration};时间t1classA{public:A(){cout::A()"::f()"案例1:时间t1classA{public:A();voidf();};A::A(){cout::A()"时间t2

c++ - 没有模板重新绑定(bind)的 typedef 模板。作为模板类参数的模板使用

我想做这样的事情:templateclassBaseSubscriber{};templateclassBasePublisher{//notworking:invaliduseoftemplate-name'BaseSubscriber'withoutanargumentlisttypedefBaseSubscriberSubscriberType;//compilingtypedefBaseSubscriberSubscriberTypeT;};templateclassSubscriber,classData>classClassA:publicSubscriber{};temp

c++ - 使用模板进行宏扩展

我正在编写几个将模板函数的结果作为输入的函数:intalg1(Vect3){...}...intalgN(Vect3){...}voidmain(){alg1(mat.topRightCorner())}哪里,如果你好奇,topRightCorner返回mat的子矩阵,来自Eigen的方法,其中维度在编译时已知时作为模板参数放置。但是使用宏创建一个“快捷方式”以在不同算法之间快速切换(因为在实际代码中该函数被多次调用),就像这样#defineALG(X)(algN(X))ALG(mat.topRightCorner())给出了一个错误,因为宏被正确扩展但不知何故被误解为有两个不同的参数

c++ - 基类的未定义模板参数

我很确定我已经在SO的某处阅读了编译器无法处理这段代码的原因,但是,经过几个小时的搜索,我仍然找不到它。相关代码如下:#includetemplateclassbase{};classderived:base{public:structmyStruct{};};intmain(){return0;}问题是解析器首先尝试生成base解析前的特化derived,因此,我收到此错误:“错误C2065:‘myStruct’:未声明的标识符”。作为一个愚蠢的把戏,我注意到如果我预先声明structmyStruct;,VS2010会停止提示。就在classderived之上.在我看来,myStru

c++ - 模板类的静态成员数组的延迟初始化

我正在编写代码来执行Gaussianintegration与n点,其中n是一个编译时间常数。对于给定的n,我知道如何计算横坐标和权重。计算必须从头开始为每个不同的n进行。.现在,我按照这些思路做一些事情://Severalstructslikethisone(laguerre,chebyshev,etc).templatestructlegendre{staticconstsize_tsize=n;staticconstdoublex[n];staticconstdoublew[n];};templatedoublegauss_quadrature(F&&f){doubleacc=0;

c++ - 每个类型的多实例计数器

听起来很简单,但我想不出合适的解决方案:对于寄存器分配器,我需要一个从0开始计数并在每个分配步骤递增的计数器。好吧,让我们把它变成一个普遍的问题(不特定于寄存器分配):我需要一个可以有多个实例的类(这很重要!)并且有一个模板化的成员函数返回一个整数,这个整数的值正在计算每次通话。界面应如下所示:classCounter{public:templateintplus1(){//?}private://whatmember?};当一个人使用计数器时,它应该像这样工作:intmain(){Countera,b;assert(a.plus1()==0);assert(a.plus1()==1)