代码之家  ›  专栏  ›  技术社区  ›  Brett Rossier

参数包中的参数计数?这里有C++ 0xSTD LIB函数吗?

  •  15
  • Brett Rossier  · 技术社区  · 15 年前

    我只是想知道C++0x STD LIB中有什么东西可以用来计算参数包中的参数数量吗?我想去掉下面代码中的字段“计数”。我知道我可以构建我自己的计数器,但看起来这显然是包含在C++ 0xSTD LIB中的东西,我想确定它还不在那里:“家庭增长的计数器实现也是最受欢迎的。”

    template<const int field_count, typename... Args> struct Entity {
        const tuple<Args...> data;
        const array<const char*, field_count> source_names;
    
        Entity() : data() {
        }
    };
    
    2 回复  |  直到 15 年前
        1
  •  34
  •   James McNellis    15 年前

    是的,你可以用 sizeof... . 从C++0X FCD(μ5.5.3/5):

    A中的标识 西泽… 表达式应命名参数包。这个 西泽… 运算符生成为参数包标识符提供的参数数。参数包被扩展(14.5.3) 西泽… 操作员。[ 例子:

    template<class... Types>
    struct count {
        static const std::size_t value = sizeof...(Types);
    };
    

    艾斯 结束示例 ]

        2
  •  5
  •   dirkgently    15 年前

    这里是 a link 那可能对你有帮助。链接的示例源:

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