简体   繁体   English

空参数包的模板特化

[英]Template specialization for an empty parameter pack

I have a variadic template function which calls itself to determine the largest number in a list (constituted by the templatized arguments). 我有一个可变参数模板函数,它调用自己来确定列表中的最大数字(由模板化参数构成)。 I am trying to make a specialization for when the parameter pack is empty so I can just return the number at the front of the list, but I don't know how to do that. 我试图在参数包为空时进行专门化,这样我就可以返回列表前面的数字,但我不知道该怎么做。 I am just becoming familiar with variadic templates and template specialization, but this is what I have so far: 我只是熟悉可变参数模板和模板专业化,但这是我到目前为止所拥有的:

#include <string>
#include <iostream>

using namespace std;

template <int N, int... N2>
int tmax() {
    return N > tmax<N2...>() ? N : tmax<N2...>();
}

template <int N>
int tmax() {
    return N;
}

int main() {
    cout << tmax<32, 43, 54, 12, 23, 34>();
}

However, this produces the following error: 但是,这会产生以下错误:

test.cpp: In function ‘int tmax() [with int N = 34, int ...N2 = {}]’:
test.cpp:9:45:   instantiated from ‘int tmax() [with int N = 23, int ...N2 = {34}]’
test.cpp:9:45:   instantiated from ‘int tmax() [with int N = 12, int ...N2 = {23, 34}]’
test.cpp:9:45:   instantiated from ‘int tmax() [with int N = 54, int ...N2 = {12, 23, 34}]’
test.cpp:9:45:   instantiated from ‘int tmax() [with int N = 43, int ...N2 = {54, 12, 23, 34}]’
test.cpp:9:45:   instantiated from ‘int tmax() [with int N = 32, int ...N2 = {43, 54, 12, 23, 34}]’
test.cpp:18:39:   instantiated from here
test.cpp:9:45: error: no matching function for call to ‘tmax()’
test.cpp:9:45: error: no matching function for call to ‘tmax()’

I have also tried this, just to see if it would work (although it introduces the number 0 to the list randomly so that it can't ever return a number less than 0): 我也试过这个,只是为了看看它是否会起作用(虽然它会随机地将数字0引入列表中,以便它不能返回小于0的数字):

template <int N, int... N2>
int tmax() {
    return N > tmax<N2...>() ? N : tmax<N2...>();
}

template <>
int tmax<>() {
    return 0;
}

However, in addition to the errors mentioned above, I get this error: 但是, 除了上面提到的错误,我收到此错误:

error: template-id ‘tmax<>’ for ‘int tmax()’ does not match any template declaration

So what should I do to get this working? 那么我该怎么办呢?

I am using g++ 4.5.2 with the -std=c++0x flag. 我正在使用带有-std=c++0x标志的g ++ 4.5.2。

I see two mistakes using clang . 我看到使用clang的两个错误。

  1. Put the overload taking a single int first. 将重载首先放在单个int中。

  2. Make things unambiguous for lists of length 1. Recall that variadic lists can have zero size and when they do, it seems to me you have an ambiguity. 使长度为1的列表明确无误。回想一下,可变列表的大小可以为0,当它们出现时,在我看来你有一个模糊性。

This compiles and runs correctly for me: 这为我编译和运行正确:

#include <iostream>

using namespace std;

template <int N>
int tmax() {
    return N;
}

template <int N, int N1, int... N2>
int tmax() {
    return N > tmax<N1, N2...>() ? N : tmax<N1, N2...>();
}

int main() {
    cout << tmax<32, 43, 54, 12, 23, 34>();
}

54 54

Personally, I'd prefer using static class members over functions for this sort of thing: 就个人而言,我更喜欢在函数中使用静态类成员来处理这类事情:

template <int... N> struct max;
template <int N, int... M> struct max<N, M...> {
  static const int value = max<N, max<M...>::value>::value;
};    
template <int N, int M> struct max<N, M> {
  static const int value = N > M ? N : M;
};

int main()
{
  return max<1,2,3>::value;
}

Update: Using ildjarn's suggestion, here's the less verbose version: 更新:使用ildjarn的建议,这里是更简洁的版本:

#include <type_traits>
template <int... N> struct max;
template <int N, int... M> struct max<N, M...>
  : std::integral_constant<int, max<N, max<M...>::value>::value> { };
template <int N, int M> struct max<N, M>
  : std::integral_constant<int, (N > M ? N : M)> { };

Since you can't partially specialize functions, you need to wrap your functionality: 由于您无法部分专门化功能,因此需要包装您的功能:

template<int Head, int... Tail>
struct Tmax{
  static int do(){
    return Head > Tmax<Tail...>::do() ? Head : Tmax<Tail...>::do();
  }
};

template<int N>
struct Tmax<N>{
  static int do(){
    return N;
  }
};

template<int... Numbers>
int tmax(){
  return Tmax<Numbers...>::do();
}

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

相关问题 问:带有参数包的模板特化 - Q: Template specialization with parameter pack 空包装时的可变参数模板类专业化 - Variadic template class specialization when empty pack 使用模板专门化拆分可变参数包 - Split variadic parameter pack using template specialization variadic模板必须具有可调用的特殊化,非空参数包才能格式正确吗? - Must variadic template have callable specialization with non empty parameter pack to be well-formed? 模板特化中模板arguments参数包扣失败 - Failed parameter pack deduction of template arguments in a template specialization 可变参数模板的歧义-空参数包 - Variadic template ambiguity - empty parameter pack 具有非空模板参数列表的模板专业化 - Template specialization with non-empty template parameter list 带有模板参数的模板专业化 - template specialization with template parameter 如何使用参数包和非类型模板值执行部分模板特化? - How to perform partial template specialization with a parameter pack and non-type template value? 对于出现在可变参数模板参数包的任何位置的类型的类模板的部分特化 - Partial specialization of class template for a type that appears in any position of a variadic template parameter pack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM