草庐IT

TEMPLATE

全部标签

c++ - 如何实现is_pointer?

我想实现is_pointer。我想要这样的东西:templateboolis_pointer(Tt){//implementation}//returntrueorfalseinta;char*c;SomeClasssc;someAnotherClass*sac;is_pointer(a);//returnfalseis_pointer(c);//returntrueis_pointer(sc);//returnfalseis_pointer(sac);//returntrue我该如何实现?谢谢 最佳答案 templatestruc

c++ - 如何在 32 位和 64 位环境中使用 intptr_t 可靠地专门化模板?

我有一个模板,我想专注于两种int类型,一种是普通的旧int,另一种是intptr_t。在64位平台上,它们有不同的大小,我可以轻松做到这一点,但在32位平台上,这两种类型是相同的,编译器会抛出有关重新定义的错误。除了使用预处理器禁用其中一个定义外,我还能做些什么来修复它?一些代码作为例子:templatetype*convert();templatetype*convert(){returngetProperIntType(sizeof(int));}templatetype*convert(){returngetProperIntType(sizeof(intptr_t));}//

c++ - 有没有办法绑定(bind) template<template> 参数?

上下文我有一个自定义比较器,它采用另一个比较器并应用额外的检查:templateclassComparator,typenameT>structSoftOrder:publicstd::binary_function{booloperator()(constTlhs,constTrhs)const{returnComparator()(lhs,rhs)&&AnotherCheck();}};我有第二个类接受比较器,例如:templateclassComparator>classProcessor{...};实例化Processor很容易使用标准比较器(例如std::less),如下所示

c++ - 如果存在同名的不相关全局模板函数,为什么不需要模板关键字?

这个问题与我之前的问题Compilererrorwhentryingtocalltemplatemethodfromprivateinstance有关,被指出与这个问题有关:WhereandwhydoIhavetoputthe"template"and"typename"keywords?所以我读了这篇文章,我明白C++语言定义是不明确的,所以它不能总是被正确解析。就我而言,答案是我需要a.templatef()在B::test()帮助解析器理解它正在处理一个模板。很好。但是,看完所有这些之后,为什么解析器突然可以不用template了?关键字,如果我碰巧有一个完全不相关的全局模板函数

c++ - 具有重载的显式模板函数特化 : Why would you do it?

假设如下:templatevoidfoo(T*);//#1templatevoidfoo(T);//#2templatevoidfoo(int*);//#3当引入一个也有重载的基本模板的显式特化时,在设计重载解析期间不考虑特化。我明白这一点。但是,鉴于我可以使#3成为非模板重载,然后它会被考虑用于重载决议,为什么我仍然想像上面那样做呢?上面演示的设置是否有有效的用例?我唯一能想到的是,如果您不依赖模板类型推导,则无法使用非模板函数,因为它们不接受。调用它们时的语法。顺便说一句,我只回顾了C++03的规则。我不确定C++11是否/如何改变这些规则/行为。 最佳

c++ - 根据模板模板参数采用的参数数量部分专门化模板的语法是什么?

考虑以下代码:templatestructOne{};templatestructTwo{};templateclassTTP,typename...>structSS;#ifdefTEST_TTPtemplateclassOneParam,typename...Ts>structSS{};templateclassTwoParam,typename...Ts>structSS{};#else//TEST_TTPtemplateclassOneParam,typenameTParam>structSS{};templateclassTwoParam,typenameTParam1,ty

c++ - 如何定义具有非尾随 decltype 返回类型的外联类模板成员函数

templatestructfoo{intx;decltype(x)f1();};似乎不可能定义f1out-of-line。我尝试了以下定义,但均无效:templatedecltype(x)foo::f1(){}templateautofoo::f1()->decltype(x){}templateautofoo::f1(){returnx;}templatedecltype(std::declval>().x)foo::f1(){}//Thisreturntypeiscopiedfromthegccerrormessagetemplatedecltype(((foo*)(void)0

c++ - 为什么显式模板实例化会在存在外线虚拟时导致 weak-template-vtables 警告?

[编辑以显示.cpp和hpp之间的拆分]//file.hppclassBase{public:virtual~Base(void);Base(void);Base(constBase&)=default;};templateclassDerived:publicBase{public:Derived(void);boolfunc(void);};//file.cpp#include"file.hpp"Base::~Base(void){}Base::Base(void){}templateboolDerived::func(void){returntrue;}templateDeriv

c++ - TT : Is that possible/correct 上的模板化类中 T 上的模板化方法

我有一个以类型名T为模板的类MyClass。但在内部,我想要一个以另一种类型TT(与T无关)为模板的方法。阅读/修补后,我发现了以下符号:templateclassMyClass{public:templatevoidMyMethod(constTT¶m);};出于风格原因(我喜欢在一个头文件中声明模板化类,在另一个头文件中定义方法),我不会在类声明中定义方法。所以,我必须把它写成:template//thisisthetypeoftheclasstemplate//thisisthetypeofthemethodvoidMyClass::MyMethod(constTT&pa

c++ - 模板代码上的编译器堆栈溢出

在处理我自己的类型删除迭代器时,我遇到了一个问题,即编译器(MSVC10)因这段代码的堆栈溢出而崩溃:structbase{};//Inactualcode,thisisatemplatestructthatholdsdatatemplatestructany;//Inactualcode,thisisabstractbasestructtemplatestructfrom;//Inactualcode,thisisfunctiondefinitionsofanytemplatestructany{voida(){}};templatestructany:publicany//comm