参数包中的参数计数?这里有一个C ++ 0x std lib函数吗? [英] Count of parameters in a parameter pack? Is there a C++0x std lib function for this?

查看:125
本文介绍了参数包中的参数计数?这里有一个C ++ 0x std lib函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有什么C ++ 0x std lib已经可用来计算参数包中的参数数量?我想摆脱下面的代码中的field_count。我知道我可以建立自己的计数器,但它只是似乎这是一个明显的事情,包括在C + + 0x std lib,我想确定它不是已经有:)自己的计数器实现

 模板 const tuple< Args ...>数据; 
const array< const char *,field_count> source_names;

Entity():data(){
}
};是的,您可以使用 sizeof ... 。从C ++ 0x FCD(§5.3.3/ 5):


sizeof中的标识符。 .. 表达式应命名一个参数包。 sizeof ... 运算符产生为参数包标识符提供的参数数。参数包由 sizeof ... 运算符扩展(14.5.3)。 [示例:

 模板< class ... Types& 
struct count {
static const std :: size_t value = sizeof ...(Types);
};

- end example ]


< blockquote>

I was just wondering if there was anything in the C++0x std lib already available to count the number of parameters in a parameter pack? I'd like to get rid of the field_count in the code below. I know I can build my own counter, but it just seems like this would be an obvious thing to include in the C++0x std lib, and I wanted to be sure it wasn't already there :) Home-grown counter implementations are most welcome too.

template<const int field_count, typename... Args> struct Entity {
    const tuple<Args...> data;
    const array<const char*, field_count> source_names;

    Entity() : data() {
    }
};

解决方案

Yes, you can use sizeof.... From the C++0x FCD (§5.3.3/5):

The identifier in a sizeof... expression shall name a parameter pack. The sizeof... operator yields the number of arguments provided for the parameter pack identifier. The parameter pack is expanded (14.5.3) by the sizeof... operator. [Example:

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

end example ]

这篇关于参数包中的参数计数?这里有一个C ++ 0x std lib函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆