简体   繁体   English

std :: tuple中的Void类型

[英]Void type in std::tuple

Obviously, you can't have an instance of type void in a well-formed program, so something like the following declaration won't compile: 显然,在格式良好的程序中你不能有一个void类型的实例,所以类似下面的声明就不会编译:

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

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: 例如,我的编译器(GCC)让我说:

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

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. 这对我很有意思,因为看起来使用C ++ 0x我们可以使用std::tuple来执行许多早期需要boost::mpl库的元编程技巧。 For example, we can use std::tuple to create a vector of types. 例如,我们可以使用std::tuple来创建类型的向量。

For example, suppose we want to create a vector of types representing a function signature: 例如,假设我们要创建表示函数签名的类型向量:

We can just say: 我们可以说:

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;
};

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 . 这似乎有效,即使函数签名具有void类型,只要我们从未实际实例化get_function_signature<F>::type的实例。

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. 但是,C ++ 0x对我来说仍然是新手,当然所有实现仍然有点实验性,所以我对此有点不安。 Can we really use std::tuple as a vector of types for meta-programming? 我们真的可以使用std::tuple作为元编程的类型向量吗?

It does actually make sense that you can do 你确实可以做到这一点

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

as long as you only use it as a type-list to use tuple_element on. 只要你只使用它作为类型列表来使用tuple_element Thus I can do 因此,我可以做到

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

which will declare param as void* 这将声明param为void*

Probably, tuple with void element is safe unless we instantiate it. 除非我们实例化,否则使用void元素的tuple可能是安全的。
So, though we can't write as the following, 所以,虽然我们不能写如下,

struct C : std::tuple< void > {...

I can't imagine the case that this usage is useful now. 我无法想象这种用法现在有用的情况。 So, it won't matter. 所以,没关系。

Well, this also applies to std::pair . 嗯,这也适用于std::pair We can write simple type list as the following: 我们可以编写简单的类型列表如下:

struct Nil;
typedef std::pair< void, std::pair< int, Nil > > t;

though somehow such pair usage seems to be rare. 虽然不知何故,这种pair使用似乎很少见。

Incidentally, tuple type list might fail in some SFINAE-like purpose. 顺便说一句, tuple类型列表可能在某些类似SFINAE的目的中失败。 For example, the following code isn't compiled on ideone(gcc-4.5.1) when I tested: 例如,当我测试时,以下代码不在ideone(gcc-4.5.1)上编译:

std::tuple< void > f();
template< class T > char g( T const& );

int main() {
  sizeof g( f() );
}

So, I'm not sure that current type lists can be replaced completely with tuple in near future. 所以,我不确定当前的类型列表是否可以在不久的将来完全替换为tuple

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM