void类型在std :: tuple中 [英] Void type in std::tuple

查看:144
本文介绍了void类型在std :: tuple中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,在一个格式良好的程序中不能有类型 void 的实例,因此类似下面的声明不会编译:

Obviously, you can't have an instance of type void in a well-formed program, so something like the following declaration won't compile:

std::tuple<void, double, int> tup;

然而,只要我们严格地处理类型而不是对象,没有任何问题。例如,我的编译器(GCC)让我说:

However, as long as we're dealing strictly with types as opposed to objects, there seems to be no issue. For example, my compiler (GCC) lets me say:

typedef std::tuple<void, double, int> tuple_type;

这很有趣,因为看起来对于C ++ 0x,我们可以使用 std :: tuple 来执行很多元编程技巧,以前需要 boost :: mpl 库。例如,我们可以使用 std :: tuple 创建一个类型的向量。

This is interesting to me, because it seems that with C++0x we can just use std::tuple to perform a lot of the meta-programming tricks that earlier would have required the boost::mpl library. For example, we can use std::tuple to create a vector of types.

例如,假设我们想要创建表示函数签名的类型的向量:

For example, suppose we want to create a vector of types representing a function signature:

我们可以说:

template <class R, class... Args>
struct get_function_signature;

template <class R, class... Args>
struct get_function_signature<R(*)(Args...)>
{
    typedef std::tuple<R, Args...> type;
};

这似乎工作,即使函数签名有 void 类型,只要我们从不实际实例化 get_function_signature< F> :: type 的实例。

This seems to work, even if the function signature has a void type, as long as we never actually instantiate an instance of get_function_signature<F>::type.

然而,C ++ 0x对我来说还是新的,当然所有的实现仍然有些实验性,所以我有点不安。我们可以使用 std :: tuple 作为元程序的类型向量吗?

However, C++0x is still new to me, and of course all implementations are still somewhat experimental, so I'm a bit uneasy about this. Can we really use std::tuple as a vector of types for meta-programming?

推荐答案

它实际上是有意义的,你可以做

It does actually make sense that you can do

typedef std :: tuple< void,double,int& tuple_type;

只要使用它作为类型列表使用 tuple_element 。因此我可以做

as long as you only use it as a type-list to use tuple_element on. Thus I can do

tuple_element< 0,tuple_type> :: type * param;

将声明param为 void *

这篇关于void类型在std :: tuple中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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