草庐IT

2Template

全部标签

C++ 模板 : cannot match the last template in variadic class template

我正在学习C++11可变参数模板并创建了一个模板结构来计算给定列表的最大数量并尝试了:#include#includetemplatestructmax:std::integral_constantb?max::value:max::value)>{};templatestructmax:std::integral_constantb?max::value:max::value)>{};templatestructmax:std::integral_constant{};intmain(){std::cout::value但是g++提示:test.cc:7:58:error:wrong

c++ - g++ 可变参数模板。简单示例代码无法编译,提示 'Not a template'

我正在探索这个陌生的领域,并想尝试来自DannyKalev'stutorialonthematter的一个简单示例.代码非常简单:templatestructCount{staticconstintvalue=0;};templatestructCount//partialspecialization{staticconstintvalue=1+Count::value;};但是gcc4.4.7甚至4.7.0提示(尽管-std=c++0x-std=gnu++0x标志):/src/tests/VTemplates.h:12:8:error:'Count'isnotatemplate/sr

c++ - 错误 C2899 : typename cannot be used outside a template declaration

我正在MSV2010中尝试以下内容namespacestatismo{templatestructRepresenterTraits,3u>>{typedefitk::Image,3u>VectorImageType;typedefVectorImageType::PointerDatasetPointerType;typedefVectorImageType::PointerDatasetConstPointerType;typedeftypenameVectorImageType::PointTypePointType;typedeftypenameVectorImageType:

C++ : friend declaration ‘declares a non-template function

我在重载时遇到问题流运算符(operator),我找不到解决方案:templateclassNVector{inlinefriendstd::ostream&operator&rhs);};templateinlinestd::ostream&NVector::operator&rhs){/*SOMETHING*/returnlhs;};它产生以下错误信息:warning:frienddeclaration‘std::ostream&operatorerror:‘std::ostream&NVector::operator如何解决这个问题?非常感谢。 最佳答

c++ - 错误 : expected primary-expression before ‘>’ : templated function that try to uses a template method of the class for which is templated

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。在使用模板和仿函数(未出现在这个问题中)时,我最终遇到了以下简化的问题。以下代码(也可用here)classA{public:templateboolisGood(intin)const{constTf;returninbooltryEvaluator(T&evaluator,intvalue){returnevaluator.isGood(value);}intmain(intargc,constchar*argv[]

C++ 如何使 template<T>f() 为整数 T 返回 -1,为指针类型返回 nullptr

我需要完成以下任务:templatef(){:return{-1ifTisofintegraltype,elsenullptr}}在我的特定用例中,T可以是四种类型之一:intPy_ssize_t//ssize_tPy_hash_t//ssize_tPyObject*//PyObjectissomeCstruct这是我迄今为止最好的解决方案:templateTtest(typenameenable_if::value,void*>::type=nullptr){return-1;}templateTtest(typenameenable_if::value,void*>::type=n

c++ - 嵌套命名空间 : where should default template arguments go? 中模板类的前向声明

我在嵌套命名空间中有一个模板类的前向声明namespacen1{namespacen2{templatestructA;}usingn2::A;}接着是一个定义,实际上它在不同的文件中,中间有一些东西:structX{};namespacen1{namespacen2{templatestructA{};}usingn2::A;}那么以下总是可以的:n1::n2::Aa;但是这个捷径n1::Aa;在clang中给出编译错误error:toofewtemplateargumentsforclasstemplate'A'除非我删除前向声明;g++两者都接受。clang似乎保留在第一个不包含

c++ - g++ 错误 : specialization after instantiation (template class as friend)

考虑以下C++代码:templateclassSingleton{};classConcreteSingleton:publicSingleton{templatefriendclassSingleton;};intmain(){}Singleton应该是ConcreteSingleton的friend:它适用于Microsoft的可视化C++编译器。但是,我不能用g++4.8.4编译它。错误是:error:specializationof‘Singleton’afterinstantiationtemplatefriendclassSingleton;有什么办法可以解决吗?

C++ template-id 不匹配任何模板?

我用模板和特化写了一个简单的代码:#includetemplateintHelloFunction(constT&a){std::coutintHelloFunction(constchar*&a){std::cout我认为char*特化是正确的,但是g++报告:D:\work\test\HelloCpp\main.cpp:11:5:error:template-id'HelloFunction'for'intHelloFunction(constchar*&)'doesnotmatchanytemplatedeclaration请帮我找出错误。 最佳答案

c++ - 错误 : C2988: unrecognizable template declaration/definition

我的两个模板的标题中出现错误。两者都有类似的声明和定义如下:templatevoidsetVideoCodecOption(T1AVCodecContext::*option,T2(CR2CVideoCodecSettings::*f)()const);templatevoidEncoderPrivate::setVideoCodecOption(T1AVCodecContext::*option,(CR2CVideoCodecSettings::*f)()const){T2value=(m_videoSettings.*f)();if(value!=-1){m_videoCodecC