Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Void type in std::tuple

Tags:

c++

c++11

tuples

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;

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;

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:

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.

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?

like image 914
Channel72 Avatar asked Feb 03 '11 10:02

Channel72


People also ask

What is a std :: tuple?

Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair. If std::is_trivially_destructible<Ti>::value is true for every Ti in Types , the destructor of tuple is trivial.

Is tuple a Constexpr?

Initializes each element of the tuple with the corresponding element of other . This constructor is constexpr if every operation it performs is constexpr. For the empty tuple std::tuple<>, it is constexpr.

Is tuple mutable in C++?

The tuple itself isn't mutable (i.e. it doesn't have any methods that for changing its contents). Likewise, the string is immutable because strings don't have any mutating methods. The list object does have mutating methods, so it can be changed.

Does C++ have tuple?

Tuples in C++A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.


1 Answers

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. Thus I can do

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

which will declare param as void*

like image 121
CashCow Avatar answered Oct 06 '22 02:10

CashCow