简体   繁体   English

模板用作默认模板参数

[英]Template functions as default template parameters

My class declaration begins as follows: 我的类声明如下所示:

template< 
    class P, 
    class B, 
    class comparePosition = compareHorizontal< P >
>
class BlahBlah  { ...

where compareHorizontal is a template function. 其中compareHorizontal是模板函数。 When I attempt to compile, clang spits out 当我尝试编译时,c发出声

[...]/entity.hpp:57:33: error: type name requires a specifier or qualifier
        class comparePosition = compareHorizontal< P >
                                ^

[...]/entity.hpp:57:33: error: C++ requires a type specifier for all declarations
        class comparePosition = compareHorizontal< P >
                                ^~~~~~~~~~~~~~~~~

(and a whole lot of other errors on the same line). (以及同一行上的很多其他错误)。

If I just remove the default template parameter, leaving all else intact, it compiles just fine. 如果我只是删除默认的模板参数,而其他所有参数保持不变,则可以正常编译。 So I wonder, how would I use a function template as a default argument, if that's even possible? 所以我想知道,如果可能的话,我将如何使用函数模板作为默认参数? Or would I be better off just making a functor class with an operator() that calls compareHorizontal and use that instead? 还是只用一个operator()来调用functor类,然后调用compareHorizontal而不是呢?

I think the reason is that the template function is not a type. 我认为原因是模板函数不是类型。 It is a specific value if you think of it, the type of the function goes something like this: 如果您认为它是一个特定的值,则函数的类型如下所示:

  template<
       class P,
       class B,
       class C=bool (*)(P&)
   >
   class BlahBlah  {
   };

and it compiles. 并编译。 It is as if you said class C=5; 好像您说的是C = 5级; this also will not compile, because 5 is not a type. 这也不会编译,因为5不是类型。 I suggest you use a struct in such case. 我建议您在这种情况下使用结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM