我在一个类中有以下(部分)代码,在该类中,我尝试通过C ++ 11中的元编程针对值列表评估值。bool eval(GLenum value){ return false;};template<GLenum One, GLenum... Others>bool eval(GLenum value){ if( value == One ) return true; // Try out the rest return eval<Others...>(value);};gcc抱怨: ../emul/GLPart.h:在成员函数“ bool”中 GLPart :: eval(GLenum)[with unsigned int One = 519u, unsigned int ... Others = {},unsigned int ... ValidEnums = {512u,513u, 514u,515u,516u,517u,518u,519u},GLenum = unsigned int]’: ../emul/GLPart.h:26:31:从'bool实例化 GLPart :: eval(GLenum)[with unsigned int One = 518u, unsigned int ... Others = {519u},unsigned int ... ValidEnums = {512u, 513u,514u,515u,516u,517u,518u,519u},GLenum = unsigned int]’ ../emul/GLPart.h:26:31:从'bool实例化 GLPart :: eval(GLenum)[with unsigned int One = 517u, unsigned int ... Others = {518u,519u},unsigned int ... ValidEnums = {512u,513u,514u,515u,516u,517u,518u,519u},GLenum =无符号 int]” ../emul/GLPart.h:26:31:从“ bool”实例化 GLPart :: eval(GLenum)[with unsigned int One = 516u, unsigned int ... Others = {517u,518u,519u},unsigned int ... ValidEnums = {512u,513u,514u,515u,516u,517u,518u,519u}, GLenum = unsigned int]’../emul/GLPart.h:26:31:实例化为 ‘bool GLPart :: eval(GLenum)[with unsigned int One = 515u, unsigned int ... Others = {516u,517u,518u,519u},unsigned int ... ValidEnums = {512u,513u,514u,515u,516u,517u,518u,519u}, GLenum = unsigned int]’../emul/GLPart.h:26:31:实例化为 ‘bool GLPart :: eval(GLenum)[,其中unsigned int One = 514u, unsigned int ... Others = {515u,516u,517u,518u,519u},unsigned int ... ValidEnums = {512u,513u,514u,515u,516u,517u,518u,519u}, GLenum = unsigned int]’../emul/GLPart.h:26:31:实例化为 ‘bool GLPart :: eval(GLenum)[with unsigned int One = 513u, unsigned int ... Others = {514u,515u,516u,517u,518u,519u}, unsigned int ... ValidEnums = {512u,513u,514u,515u,516u,517u, 518u,519u},GLenum = unsigned int]’../emul/GLPart.h:26:31: 从'bool GLPart :: eval(GLenum)[ unsigned int一个= 512u,unsigned int ...其他= {513u,514u,515u, 516u,517u,518u,519u},无符号整数... ValidEnums = {512u,513u, 514u,515u,516u,517u,518u,519u},GLemum = unsigned int]’ ../emul/GLPart.h:31:43:从'bool实例化 GLPart :: Evaluate(GLenum)[具有未签名的int ... ValidEnums = {512u,513u,514u,515u,516u,517u,518u,519u},GLenum =无符号 int]’alpha.cpp:8:7:从此处实例化../emul/GLPart.h:26:31: 错误:没有匹配的函数可以调用‘GLPart 515u,516u,517u,518u,519u> :: eval(GLenum&)’因此,当“一个”具有值而“其他”不具有值时,它似乎在上次递归时感到窒息。在这种情况下,模板参数应为空。我是否需要以其他方式声明普通评估?有一阵子没有编码C ++了,所以它可能微不足道,但我只是不明白;)当尝试将template 添加到第一个评估中时,它会阻塞: ../emul/GLPart.h:14:11:错误: 非命名空间范围“ class GLPart” ../emul/GLPart.h:21:7: 错误:模板参数列表太多../emul/GLPart.h:成员中 函数“ bool GLPart :: Evaluate(GLenum)”: ../emul/GLPart.h:32:23:错误:参数包未使用 ‘...’:../ emul / GLPart.h:32:23:注意:“ ValidEnums” ../emul/GLPart.h:32:33:错误:在“ ...”令牌之前应为“,”或“;”解:template<GLenum One>bool eval(GLenum value){ return value == One;};template<GLenum One, GLenum Two, GLenum... Others>bool eval(GLenum value){ if( eval<One>(value) ) return true; // Try out the rest return eval<Two, Others...>(value);}; (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 由于这往往会产生许多歧义错误,因此似乎毫无歧义的变化看起来像这样:template<GLenum One>bool eval(GLenum value){ return value == One;};template<GLenum One, GLenum Two, GLenum... Others>bool eval(GLenum value){ if( value == One ) return true; // Try out the rest return eval<Two, Others...>(value);};第一个重载仅使用一个参数,第二个重载至少使用两个参数。无论如何,接受零参数可能毫无意义。关于c++ - 元编程中最后一次递归的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7134003/ (adsbygoogle = window.adsbygoogle || []).push({}); 10-09 15:55