草庐IT

Archive_Template

全部标签

c++ - 相当于 `using` s 的 `template` 别名

C++11添加了别名模板,例如:templateusingidentity=T;templateusingEnableIf=typenamestd::enable_if::type;这些比旧的template类型映射更容易使用,后者在::type字段中为您提供返回值,因为即使您的类型参数依赖于本地上下文,您不需要通知编译器结果是一个类型。实际上,您将typename从使用位置提升到using别名。是否有任何等效的东西可以用来摆脱产生的无关模板?假设您有一个元函数,其输出是类或别名模板而不是类型。目前的方法是这样的:templatestructmy_meta{templateusingT

c++ - Q : Template class that takes either a normal type or a template template argument

最近我设计了元类型和允许编译时类型连接的可能操作:#includetemplatetypenameT>structMetaTypeTag{};/*variabletemplatehelper*/templatetypenameT>constexprMetaTypeTagmeta_type_tag={};templatestructTypeTag{};/*comparison*/templateconstexprbooloperator==(TypeTag,TypeTag){returntrue;}templateconstexprbooloperator==(TypeTag,TypeT

c++ - template-name<TT> 是推导的上下文吗?

[temp.deduct.type]paragraph8列出所有推断的上下文,但它似乎不包括template-name其中template-name指的是类模板和TT引用模板模板参数。这是推导上下文吗?如果是,为什么?如果不是,请考虑以下代码:templateclassU,templateclassV>structfoo{};templateclassU>structfoo{};intmain(){}此代码编译underClang7.0.0和GCC8.0.1,这意味着编译器认为部分特化比主模板更特化,这意味着U和V在主模板中成功推导出foo.这是编译器错误吗?

c++ - 这个 'missing template arguments' C++ 错误是什么意思

啊,C++模板...ThecodeIsee,makessensetome,butGCC...itdisagrees.以下代码按预期编译和运行,但如果您取消注释#define,则会出现我不理解的错误。符号iterator仍然只有一件事可以引用:父类(superclass)中的typedef。所以我想我有两个问题:1.错误是什么意思?2.修复它们的最佳方法是什么。#include#include#includeusingnamespacestd;//#defineWITH_TEMPLATE1#ifdefWITH_TEMPLATEtemplatestructMyClass:publicmap

c# - P/从 C# 调用 C++ template<T> 方法

我已经在C++函数中定义了外部调用:templatevoid__declspec(dllexport)SwapMe(T*fisrt,T*second){std::cout我想在C#程序中使用它。我试过这种方式:unsafeclassProgram{[DllImport("lib1.dll",EntryPoint="SwapMe")]staticexternvoidSwapMe(Tfirst,Tsecond);...}但是,我收到这样的错误:泛型方法或泛型类中的方法是内部调用、PInvoke,或在COMImport类中定义。似乎是,C#中的泛型是托管类型,在C++中具有非托管模板的体系结

c++ - 关键字 'template' 混淆了 MSVC

关于模板的“哪个编译器是正确的”问题之一。考虑以下内容:templateclassContainer{public:templateclassiterator;};templatetemplateclassContainer::iterator{public:iterator&operator++();};现在为operator++提供定义离线它看起来像这样:templatetemplatetypenameContainer::templateiterator&Container::iterator::operator++(){//doyourthingreturn*this;}果然几

c++ - 在不需要时使用 "template"和 "typename"消歧器

Thisquestion涵盖何时以及为何在C++模板代码中需要typename和template消歧器。在C++03中不需要这些消歧器的情况下使用它们是否有效?在C++11中怎么样? 最佳答案 对于“有效”的某些定义,它在符合C++03/C++11的编译器中有效。C++03ISO/IEC14882:2003§14.2.5:[Note:justasisthecasewiththetypenameprefix,thetemplateprefixisallowedincaseswhereitisnotstrictlynecessary;i

c++ - Clang (OS X) 在特定的嵌套声明中需要 "template"关键字,而 VS 禁止它

我正在使用两个编译器(Xcodev5.0.2上的Clang和VisualStudio2012Update4)编写一个跨平台应用程序,我遇到了两个编译器不同意使用所需语法的场景嵌套声明中的template关键字。这是代码(归结为一个易于重现的测试用例):templatestructBase{templatestructInnerBase{};};templatestructDerived:publicBase{//the"template"keywordisREQUIREDinClang/OSXstructInnerDerived:publicBase::templateInnerBas

c++ - "template"关键字之前的 "class"关键字在做什么?

只是一些代码示例[不是现实生活中的例子]//atfilescopetemplatestructdemo{};templateclassdemo;//isthetemplatekeywordoptionalhere?第3行中的模板关键字是可选的吗?我以前没有(经常)看到模板关键字的这种用法。标准的哪一部分说允许这样做?编辑我认为g++有一个错误。templatestructdemo{};classdemo;//templatekeywordomitted在g++(4.5.1)上编译而在Comeau上失败"ComeauTest.c",line5:error:specializingclas

c++ - 没有参数的模板类 `template<>` 是什么意思?

没有参数的模板类是什么意思?例如,让我们以计算阶乘的模板类为例,其模板参数为N-N!。.基本上,这是类:templateclassFactorial{public:enum{fact=N*Factorial::fact};};但是,我发现这个类有一个“扩展类”,templateclassFactorial{public:enum{fact=1};};我的问题是:没有参数的模板是什么,template什么意思?提前致谢。 最佳答案 这个templateclassFactorial{public:enum{fact=1};};实际上是类