c++ - 计算嵌套包中的类型总数

标签 c++ templates c++11 variadic

NumTypes<Args...>::value是给出 Args... 中的类型总数,包括嵌套包中的所有类型(如果有),例如如果

using T = Group<int, bool, Wrap<char, Pack<char, long, Group<char, Object, short>, short>, double>, long>;

然后 NumTypes<T, int, char>::value将是 13(我们不计算包装类本身)。以下代码工作正常,但是当我用 std::string 替换任何类型时我得到一阵 std::allocator永远不会终止的错误(使用 GCC 4.8.1)。我怀疑其他类型会产生相同的错误。为什么?以及如何修复代码以避免出现这种奇怪的错误?

#include <iostream>
#include <string>

#define show(variable) std::cout << #variable << " = " << variable << std::endl;

template <typename T>
struct IsPack {
    static const bool value = false;
};

template <template <typename...> class P, typename... Args>
struct IsPack<P<Args...>> {
    static const bool value = true;
};

template <typename...> struct NumTypes;

template <typename T>
struct NumTypes<T> {
    static const int value = 1;
};

template <template <typename...> class P>
struct NumTypes<P<>> { static const int value = 0; };

template <template <typename...> class P, typename First, typename... Rest>
struct NumTypes<P<First, Rest...>> {
    static const int value = IsPack<First>::value ?
        NumTypes<First>::value + NumTypes<P<Rest...>>::value :
        1 + NumTypes<P<Rest...>>::value;
};

template <typename First, typename... Rest>
struct NumTypes<First, Rest...> {
    static const int value = NumTypes<First>::value + NumTypes<Rest...>::value;
};

template <typename...> struct Pack;
template <typename...> struct Group;
template <typename...> struct Wrap;
struct Object {};

int main() {
    using A = Pack<int, Object, long>;
    show (NumTypes<A>::value)  // 3

    using B = Pack<int, bool, Pack<char, Object>, long>;
    show (NumTypes<B>::value)  // 5

    using C = Group<int, bool, Wrap<char, Pack<char, long, Group<char, Object, short>, short>, double>, long>;
    show (NumTypes<C>::value)  // 11

    using D = Group<Pack<int, Object, double>, bool, Wrap<char, Pack<char, double, Group<char, Pack<char, long, short>, int, Object>, short>, double>, long>;
    show (NumTypes<D>::value)  // 16

    std::cout << NumTypes<A, B, int, char, C, Object, D>::value << std::endl;  // 38
}

最佳答案

std::string实际上是std::basic_string<char, char_traits<char>, allocator<charT>> .
它的声明有默认的模板参数:

template <class charT,
          class traits = char_traits<charT>,
          class Alloc = allocator<charT>>
class basic_string;

你的递归是如此错误,而且你有无限循环(当你从 basic_string<charT, traits, Alloc> 中删除第一个参数时,你得到了 basic_string<traits, Alloc, allocator<traits>> )。

您可以通过删除 P 来解决这个问题从等式:

template <template <typename...> class P, typename First, typename... Rest>
struct NumTypes<P<First, Rest...>> {
    static const int numInFirst = NumTypes<First>::value;
    static const int value = numInFirst + NumTypes<std::tuple<Rest...>>::value;
};

Live example

关于c++ - 计算嵌套包中的类型总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092422/

相关文章:

c++ - 不同类型的运算符

javascript - 在 django 项目中放置 Angular ng-templates 的位置

c++ - Boost Asio - 消息内容传输错误

c++ - 绑定(bind)到引用时临时对象生命周期扩展异常的基本原理是什么?

algorithm - 从顶点初始化半边数据结构

c++ - 为什么编译器不会自动将看似 float const 值假定为 float

c++ - 错误 : cast from 'const prog_uchar*' to 'byte' loses precision?

c++ - 我可以将 multimap 迭代逻辑放到另一个函数中吗?

c++ - struct 的字段作为模板参数

c++ - 我在编译时收到这个 "error: ' string' not declared"