草庐IT

Archive_Template

全部标签

c++ - "missing template argument"是什么意思?

我对C++和这个站点还很陌生,所以肯定会出现错误。当我尝试编译我的代码时,我会收到类似error:missingtemplateargumentbefore'b'之类的错误。几个小时以来,我一直在世界各地寻找答案,结果把我带到了这里。我的任务是实现一个模板化的类Collection来存储一个集合使用数组的对象,沿与集合的当前大小。#include#include"collection.h"usingnamespacestd;vintmain(intargc,char*argv[]){collectionb;//#ifndefCOLLECTION_H#defineCOLLECTION_H

C++ OO设计: Inheritance of template parameter

我有一个以Base为基类的继承链。我希望能够编写一个继承Base和可能的另一个Base派生类的类模板。我可以使用虚拟继承,但我找到了另一种解决方案。我想知道它是否是常见的/可观的/合法的类设计:编写一个类模板,其中模板参数是它派生的类,即它必须是Base或Base派生类。在构造函数中,我可以使用静态断言来真正确保用户没有使用任何非法类作为模板参数。如果它有效,我将永远不会有虚拟继承问题......问题是,这样做是可以的。我在其他项目中从未见过它,所以我想在使用它之前先确定一下。编辑:为了确保我不会混淆你,这里有一些代码:classBase{};classDerived:publicBa

c++ - C-回调函数模板 : explicitly instantiate template

前提我正在使用一个提供以下接口(interface)的C库(来自C++):voidregister_callback(void*f,void*data);voidinvoke_callback();问题现在,我需要将函数模板注册为回调,这给我带来了问题。考虑以下代码:templatevoidmy_callback(void*data){…}intmain(){intft=42;register_callback(reinterpret_cast(&my_callback),&ft);invoke_callback();}这给了我以下链接器错误(在OSX上使用g++(GCC)4.5.1但

c++ - 哪个编译器是对的?需要模板化返回类型之前的 'template'?

Thissnippet(取自thisquestion)使用g++编译得很好(如图所示),只要template在返回类型之前存在。相比之下,VC10不编译该代码并出现以下错误:errorC2244:'A::getAttr':unabletomatchfunctiondefinitiontoanexistingdeclaration如果我删除template,VC10很高兴,但g++会报错:error:non-template'AttributeType'usedastemplatenote:use'A::templateAttributeType'toindicatethatitisat

c++ - 这个 "if e is a pack, then get a template name, otherwise get a variable name"是否有效?

我尝试构建一个不需要typename或template的案例,但仍会根据给定名称t生成变量或模板是否为函数参数包templatestructA{templatestaticvoidf(int){}};templatestructA{staticconstintf=0;};templateusingtype=int;templatevoidf(Tt){A...)>::f(1);}intmain(){f(1);}以上将引用staticconstint,并进行比较。以下刚好有Tt变成了一个包并制作f引用模板,但GCC也不喜欢templatevoidf(T...t){A...)>::f(1);

c++ - MSVC++ : template's static_assert is not triggered inside a lambda

更新2:这已在VS2019Preview16.1Preview1中得到修复。更新:我已在visualstudio.com提交错误报告.所以我开始研究C++的模板,当我试图阻止使用static_assert编译模板类时遇到了这个问题。基本上,static_assert错误在VS2017上使用C++语言标准:ISOC++17标准(/std:c++17)。我也在gcc-7上使用-std=c++17进行了尝试,并触发了错误。这是VS2017上的错误还是我遗漏了什么?代码示例:#include#include#includetemplateclassIntegralContainer{stati

c++ - 为什么可以在命名空间 block 之外定义 template<T> 而不是 template<>?

这是一些无法编译的代码。namespacens{classfoo{templateintbar(T*);};}templateintns::foo::bar(T*)//thisisOK{return0;}templateintns::foo::bar(int*)//thisisanerror{return1;}错误是:“'templateintns::foo::bar(T*)'在不同命名空间[-fpermissive]中的特殊化来自'templateintns::foo::bar(T*)的定义”这是一个可以编译的版本:namespacens{classfoo{templateintba

c++ - 使用 "extern template"时专门化模板的正确方法是什么?

我希望有人能指出在使用“extern模板类”和“模板类”进行显式实例化的gnuc++时,在模板类中专门化方法的正确方法。我试图用模仿我真正问题的最简单的例子来解决这个问题。似乎声明“外部模板”意味着模板实例化,它在专门化方法时会导致错误。给定一个驱动程序:main.cc#includeA_H#includeintmain(){Aai;Aal;std::cout以及以下A的实现啊。templatestructA{intget()const;};externtemplateclassA;externtemplateclassA;a.cc#include"a.h"templateintA::

c++ - 错误消息 "undefined reference to template function passed as template parameter"

当我将模板函数作为基类的模板参数传递时,链接器提示它无法链接该函数:#includetemplateinlineintidentity(){returnI;}//templateinlineintidentity(){return20;}templateclassBase{public:intf(){returnfn();}};templateclassDerived:publicBase>{public:intf2(){returnf();}};intmain(intargc,char**argv){Derivedo;printf("result:%d\n",o.f2());retu

node.js - 我到底应该如何处理 webpack 上的 "module.exports = ' html_template_content'"

所以我想用webpack做一个非常简单的任务。我有一些静态HTML模板,例如test.htmltemplatecontent我想要做的就是返回模板内的字符串例如require("raw!./test.html")with应该返回一个类似的字符串:"templatecontent"但相反,它返回以下字符串"modules.exports=templatecontent"我尝试了几个模块,例如raw-loader和html-loader。并且它们的行为方式相同。所以我查看了源代码,只是为了发现它的SUPPOSED行为方式。sowhatexactlyamIexpectedtodowithth