2

Here's an issue I ran across while playing with variadic templates. I have some code that uses specialization to count "interesting" types in a parameter pack like so:

template<typename... _Pp>
struct count;

template<>
struct count<>
{
  static const int value = 0;
};

// ignore uninteresting types
template<typename _First, typename... _Rest>
struct count<_First, _Rest...>
{
  static const int value = count<_Rest...>::value;
};

// add 1 for a pointer
template<typename _First, typename... _Rest>
struct count<_First*, _Rest...>
{
  static const int value = 1 + count<_Rest...>::value;
};

// add 1 for a reference
template<typename _First, typename... _Rest>
struct count<_First&, _Rest...>
{
  static const int value = 1 + count<_Rest...>::value;
};

// add 1 for an int
template<typename... _Rest>
struct count<int, _Rest...>
{
  static const int value = 1 + count<_Rest...>::value;
};

This code works fine, but I run into problems if I want to use the same approach to count class templates:

// add 1 for a vector
template<typename... _Rest>
struct count<vector, _Rest...>
{
  static const int value = 1 + count<_Rest...>::value;
};

The above code fails to compile, error is "expected a type, got 'vector'" on the line beginning with "struct count". I'm also unable to something simpler, all class templates accepting a single argument:

// add 1 for a class template with 1 type parameter
template<template<typename> class _First, typename... _Rest>
struct count<_First, _Rest...>
{
  static const int value = 1 + count<_Rest...>::value;
}

This code also fails to compile, complaining of "expected a type, got '_First'" once again on the line beginning with "struct count". Is someone familiar with a way to accomplish this goal using this approach (i.e. some modification that I can make to one or both of the specializations that will get them to compile and perform the desired calculation at compile time)?

EDIT: I want the parameter pack for vector to be unbound, similar to the following code for a simple container wrapper with variadic template-template parameters that also specializes on std::vector:

// pass a container as a parameter using variadic template-template 

parameter
template<typename _Tp, template<typename...> class _C>
struct success
{
  // not specialized for any container
  static const bool is_specialized = false;
  // data member of container type
  _C<_Tp> c_;
};

// partial specialization of above for std::vector
template<typename _Tp>
struct success<_Tp, std::vector>
{
  // specialized for vector
  static const bool is_specialized = true;
  // again, data member of container type
  std::vector<_Tp> c_;
};

EDIT Seems like the final answer is that what I want to do can't be accomplished, but I have found a way to reframe the problem so that I cans solve it. Many thanks to those who helped.

4

3 回答 3

2

如果我正确理解你想要什么......是的,你可以创建一个可以计算“类模板”的模板结构,这样你就可以编写类似的东西

 count<std::vector, std::map, std::set, std::pair>::value

但是你不能混合类模板和简单的类型名,所以你不能写类似的东西

 count<std::vector, int &, float, std::set>::value

问题是,如果你定义

 template <typename... _Pp>
    struct count;

你可以传递std::vector<int>给它,因为std::vector<int>is a typename,但你不能传递std::vector给它,因为std::vectoris not a typename; 它是一个template<typename...> class(或模板模板),它是完全不同的东西。

您可以编写如下内容struct countC

template <template<typename...> class ...>
struct countC;

template <>
struct countC<>
 { static const int value = 0; };

// ignore uninteresting templates
template<template<typename...> class F, template<typename...> class ... R>
struct countC<F, R...>
 { static const int value = countC<R...>::value; };

template <template<typename...> class ... R>
struct countC<std::vector, R...>
 { static const int value = 1 + countC<R...>::value; };

以下是一个完整的工作示例,我重写了您struct countstruct countT计数选定类型,我添加了一个struct countC计数选定的“类模板”,我添加了一个struct countV计数固定类型名的选定值。

#include <map>
#include <set>
#include <vector>
#include <utility>
#include <iostream>

// countC for templates

template <template<typename...> class ...>
struct countC;

template <>
struct countC<>
 { static const int value = 0; };

// ignore uninteresting templates
template<template<typename...> class F, template<typename...> class ... R>
struct countC<F, R...>
 { static const int value = countC<R...>::value; };

template <template<typename...> class ... R>
struct countC<std::vector, R...>
 { static const int value = 1 + countC<R...>::value; };

template <template<typename...> class ... R>
struct countC<std::map, R...>
 { static const int value = 1 + countC<R...>::value; };

template <template<typename...> class ... R>
struct countC<std::pair, R...>
 { static const int value = 1 + countC<R...>::value; };


// countV for for values of a fixed type

template <typename T, T ... v>
struct countV;

template <typename T>
struct countV<T>
 { static const int value = 0; };

// ignore uninteresting values
template <typename T, T f, T ... r>
struct countV<T, f, r...>
 { static const int value = countV<T, r...>::value; };

// count only int odd values
template <int f, int ... r>
struct countV<int, f, r...>
 { static const int value = (f % 2) + countV<int, r...>::value; };


// countT for typenames

template <typename...>
struct countT;

template <>
struct countT<>
 { static const int value = 0; };

// ignore uninteresting types
template <typename F, typename ... R>
struct countT<F, R...>
 { static const int value = countT<R...>::value; };

template <typename F, typename ... R>
struct countT<F*, R...>
 { static const int value = 1 + countT<R...>::value; };

template<typename F, typename ... R>
struct countT<F&, R...>
 { static const int value = 1 + countT<R...>::value; };

template<typename ... R>
struct countT<int, R...>
 { static const int value = 1 + countT<R...>::value; };


int main()
 {
   std::cout << "countC vector + map + set + pair                   = " 
      << countC<std::vector, std::map, std::set, std::pair>::value
      << std::endl;

   std::cout << "countT int + float + bool* + double& + bool + int& = " 
      << countT<int, float, bool*, double&, bool, int&>::value
      << std::endl;

   std::cout << "countV int, 1 + 4 + 4 + 5 + 7 + 10 + 11 + 16 + 15  = " 
      << countV<int, 1, 4, 4, 5, 7, 10, 11, 16, 15>::value
      << std::endl;

   std::cout << "countV long, 1 + 4 + 4 + 5 + 7 + 10 + 11 + 16 + 15 = " 
      << countV<long, 1, 4, 4, 5, 7, 10, 11, 16, 15>::value
      << std::endl;

   return 0;
 }

ps:对不起我的英语不好。

于 2016-07-05T00:14:56.517 回答
1

它应该是:

template<typename... _Rest, typename... T>
struct count<std::vector<T...>, _Rest...>
{
    static const int value = 1 + count<_Rest...>::value;
};

通用版本:

template<template<typename...> class C, typename... _Rest, typename... T>
struct count<C<T...>, _Rest...>
{
    static const int value = 1 + count<_Rest...>::value;
};

可变参数包很重要。

于 2016-06-24T06:30:02.887 回答
1

这样的事情怎么样?

// add 1 for a vector
template<typename... _Rest, typename T>
struct count<vector<T>, _Rest...>
{
  static const int value = 1 + count<_Rest...>::value;
};

和这个?

// add 1 for a class template with 1 type parameter
template<template<typename> class _First, typename T, typename... _Rest>
struct count<_First<T>, _Rest...>
{ 
  static const int value = 1 + count<_Rest...>::value;
};
于 2016-06-24T05:13:09.967 回答