Unwrapping variadic template structs - c++

I'm trying to create an variant struct, i.e. a struct that contains one of so many types. Here is my attempt so far:
template <typename Type, typename... Rest> struct OneOf {
union {
Type value;
OneOf<Rest...> rest;
};
};
template <typename Type> struct OneOf {
Type value;
};
Sadly, this doesn't compile. When I try to instantiate it, I get:
one_of.h:34:33: error: redeclared with 1 template parameter template
struct OneOf {
Is there a way to terminate a self referencing recursion with structs?

You have to first declare the primary template, and then declare any specializations (either full or partial). The primary template determines the number and kind of template arguments. When it comes time to instantiate the template, a full specialization will be used if it matches exactly, or the best-matching partial specialization if any match, otherwise, the primary template will be instantiated.
If you want OneOf to be a template that takes any number of type template arguments (0 or more), then you should declare the primary template accordingly:
template <class... T> struct OneOf;
Then you'll need two specializations: one for the base case of the recursion, which can be taken to be the empty pack:
template <>
struct OneOf<> {};
and one for the recursive case, with at least one template parameter:
template <typename Type, typename... Rest> struct OneOf<Type, Rest...> {
union {
Type value;
OneOf<Rest...> rest;
};
};
Notice that both full and partial specializations require a template argument list after the template name. If you omit this, the compiler will think you are redeclaring the primary template, which causes the error you're seeing.

I assume you are trying to write a specialization.
This is the syntax:
template <typename Type> struct OneOf<Type> {
// ^~~~~~
Type value;
};

What about
template <typename...>
struct OneOf;
template <typename Type, typename... Rest>
struct OneOf<Type, Rest...> {
union {
Type value;
OneOf<Rest...> rest;
};
};
template <>
struct OneOf<> {
};
?
O also
template <typename, typename...>
struct OneOf;
template <typename T0, typename T1, typename ... Ts>
struct OneOf<T0, T1, Ts...> {
union {
T0 value;
OneOf<T1, Rest...> rest;
};
};
template <typename T0>
struct OneOf<T0> {
T0 value;
};
?

Related

Extract first template parameter type from any object

Suppose I had a templated object in C++ Test<T> and I wanted to find out what the T value is so that when I pass Test<T> as a template argument to TestWrapper<Test<T>>, I can declare a variable called T extradata in the object TestWrapper that is the same as Test's T type.
How can I achieve this by only modifying TestWrapper (i.e. making no changes to Test struct) ? Additionally is it possible to modify it so that it will extract from any object that takes 1 template parameter e.g. foo<T>, bar<T>, rather then just Test<T>>?
Thanks in advance.
template<typename T>
struct Test {
T value;
};
//template -> extract first type parameter from any object as T
struct TestWrapper {
T extradata;
};
int main() {
TestWrapper<Test<int>> temp;
temp.extradata = 123; // temp.extradata is a int, deduced from Test<int>
}
You can use partial template specialization to do that.
template<typename T>
struct Test {
T value;
};
template <typename T>
struct TestWrapper;
template <template <typename> typename Outer, typename T>
struct TestWrapper<Outer<T>> {
T extradata;
};
int main() {
TestWrapper<Test<int>> temp;
temp.extradata = 123; // temp.extradata is a int, deduced from Test<int>
}
In this example we have not defined any default definition for TestWrapper, so it will fail to compile if you give something that does not match the specialization.
You can
// primary template
template <typename T>
struct TestWrapper {
T extradata; // declare extradata with type T
// how to declare it depends on your intent
};
// partial specialization
template <template <typename...> class C, typename T, typename... Args>
struct TestWrapper<C<T, Args...>> {
T extradata; // T is the 1st template parameter of C here
};
You can create a helper class to extract the first parameter of the instantiated template, for example:
template<class>
struct Extract;
template<template<class...> class V, class First, class... Rest>
struct Extract<V<First, Rest...>> {
using type = First;
};
template<class T>
struct TestWrapper {
typename Extract<T>::type extradata;
};
Demo.

Template template partial specialization failure: "expected a class template"

This example code generates expected a class template, got std::pair <_T1, _T2>. I tried using struct Struct <std::pair> {};, but then parameters T and M become undeducible. How to avoid this?
template <template <class...> class>
struct Struct {};
template <class T, class M>
struct Struct <std::pair <T, M>> {};
Depending of what you want
template <template <class...> class>
struct Struct {};
template <>
struct Struct <std::pair>
{
// Specialization
};
or
template <typename> struct Struct {};
template <typename First, typename Second>
struct Struct <std::pair<First, Second>>
{
// Specialization
};
That is not a valid specialization for your template.
The reason why is because std::pair<T, M> is a full specialization of the class template std::pair and therefore a class. Your template expects a class template parameter which is exactly what the compiler is telling you.

What can you do with templates with zero template parameters?

I learned some time ago that you can create templates with zero parameters. While it is not possible to create them directly, you can use member templates
template<typename ...T>
struct Maker {
template<T...>
struct HasNParams { };
};
Maker<>::HasNParams<> hnp;
I wonder whether this is intended to be well-formed and what you can do with these beasts. Can you pass them as template arguments, and create explicit specializations (I guess the only scenario is for the empty case then)?
At the risk of sounding obvious, ending recursive instantiation.
template<typename Arg, typename ...T>
struct Maker : public Maker<T...>
{
template<T...>
struct HasNmin1Params { };
};
The point here is that the actual argument list to Maker isn't empty, but we only use N-1 arguments in HasNminOneParams.
Consider the following class template:
template <typename... > struct typelist { };
This is the metaprogramming equivalent of a container. And in the same way that it is useful to have an empty vector or map, it is useful to have an empty typelist. That is, something of type typelist<>. Here are two example use-cases for such a construct.
It could be the termination condition for type recursion:
void foo(typelist<> ) { }
template <typename T, typename... Ts>
void foo(typelist<T, Ts...> ) {
bar<T>();
foo(typelist<Ts...>{});
}
It could be a "return" value for a metafunction, indicating a failure condition.
template <typename F, typename T>
struct filter_one
: std::conditional_t<F::template apply<T>::value,
typelist<T>,
typelist<>>
{ };
Which is a helper we could use to write a typelist filter metafunction:
template <typename F, typename TL>
struct filter;
template <typename F, typename... Ts>
struct filter<F, typelist<Ts...>>
: concat_t<filter_one<F, Ts>...>
{ };
Both of those are very useful features of typelist<>, and that's just the one class template.

Find template type of a template type c++

I would like to have a function that can take many different things (for simplicity) like so:
template <typename T>
typename type_to_return<T>::type // <-- Use type_to_return to get result type
foo(T t)
{
return typename type_to_return<T>::type(T); // <-- Build a thing!
}
I would then specialize the type_to_return class for the types I have created. This would make the entry be one function and I could then just define new type_to_returns and constructors.
I want type_to_return<T>::type to be just T if T is not some class template. Otherwise I want it to be that class's first template parameter. So for int, I get back int, and for MultOp<float,int,double>, I want to get back float.
How do I do that? I think I need to do something like:
// Base version
template <typename T>
struct type_to_return
{
typedef T type;
};
// Specialized type
template <template <typename> class T>
struct type_to_return<T <any_type_somehow> >
{
typedef template boost::magic_type_unwrapper<T>::param<1>::type type;
};
You may implement a type_unwrapper as follow:
template <typename T>
struct type_unwrapper;
template <template <typename...> class C, typename... Ts>
struct type_unwrapper<C<Ts...>>
{
static constexpr std::size_t type_count = sizeof...(Ts);
template <std::size_t N>
using param_t = typename std::tuple_element<N, std::tuple<Ts...>>::type;
};
which works as long there is no template value as in std::array<T, N>.
Note also that stl container declare some typedef to retrieve there template arguments as std::vector<T, Alloc>::value_type which is T.

Partial Specialization of tuple contents with variadic arguments

Currently, I'm trying to get some code to react differently to different types. This isn't the exact code, but it gets the message across.
template<class A, class B>
struct alpha {
enum { value = 0 };
};
template<class T, class... Args>
struct alpha<std::tuple<Args...>, T> {
enum { value = 1 };
};
// This gets ignored
template<class T, class... Args>
struct alpha<std::tuple<Args..., std::vector<T> >, T> {
enum { value = 2 };
};
// This gets ignored
template<class T, class... Args>
struct alpha<std::tuple<Args..., T>, T> {
enum { value = 3 };
};
template<class T, class... Args>
struct alpha<T, std::tuple<Args...> > {
enum { value = 4 };
};
template<class... LArgs, class... RArgs>
struct alpha<std::tuple<LArgs...>, std::tuple<RArgs...> > {
enum { value = 5 };
};
int main(int argc, char* argv[]) {
std::cout << alpha<std::tuple<int, double>, double>::value << std::endl; // prints 1
return 0;
}
I've tried more than this code shows, but nothing works so far and I ran across a problem with explicit specialization in a non-namespace scope. For reference, I'm working on gcc 4.6 (the one that comes with oneiric server), which I believe has complete variadic template support. I don't care how ugly it gets if the implementation works to detect the last argument of the parameter pack and the other types as well. Any suggestions?
EDIT:
I wanted to share the solution I used based on the answers (this is an example).
template<typename T> struct tuple_last;
template<typename T, typename U, typename... Args>
struct tuple_last<std::tuple<T,U,Args...>> {
typedef typename tuple_last<std::tuple<U,Args...>>::type type;
};
template<typename T>
struct tuple_last<std::tuple<T>> {
typedef T type;
};
namespace details {
// default case:
template<class T, class U>
struct alpha_impl {
enum { value = 1 };
};
template<class T>
struct alpha_impl<T, T> {
enum { value = 101 };
};
template<class T>
struct alpha_impl<T, std::vector<T>> {
enum { value = 102 };
};
// and so on.
}
template<class T, class... Args>
struct alpha<std::tuple<Args...>, T>
: details::alpha_impl<T, tuple_last<std::tuple<Args...>>;
If you compile using clang, it helpfully reports that (2) and (3) are unusable. The warning for (3), which you expect to be selected, is as follows:
warning: class template partial specialization contains a template parameter that can not be deduced; this partial specialization will never be used
struct alpha<std::tuple<Args..., T>, T> {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: non-deducible template parameter 'Args'
template<class T, class... Args>
^
Why is Args not deducible? The C++0x FDIS states at ยง14.8.2.5/9:
If the template argument list of [a type that is specified in terms of template parameters] contains a pack expansion that is not the last template argument, the entire template argument list is a non-deduced context.
In your specialization, the type std::tuple<Args..., T> is a type that is specified in terms of template parameters Args and T. It contains a pack expansion (Args...), but that pack expansion is not the last template argument (T is the last template argument). Thus, the entire template argument list of the tuple (the entirety of <Args..., T>) is a non-deduced context.
The argument list of the std::tuple is the only place in the template specialization's argument list that Args appears; since it is not deducible from there, it is not deducible at all and the specialization will never be used.
Matthieu M. provides a clever workaround in his answer.
#James provided the why, now let's try to find an alternative.
I would suggest using another level of indirection.
1. Getting the last argument
template <typename T> struct Last;
template <typename T, typename U, typename... Args>
struct Last<std::tuple<T,U,Args...>>
{
typedef typename Last<std::tuple<U,Args...>>::type type;
};
template <typename T>
struct Last<std::tuple<T>>
{
typedef T type;
};
2. Introducing a specialized helper
template <typename T, typename U>
struct alpha_tuple
{
enum { value = 1 };
};
template <typename T>
struct alpha_tuple<T,T>
{
enum { value = 3 };
};
template <typename T>
struct alpha_tuple<std::vector<T>,T>
{
enum { value = 2; }
};
3. Hooking it up
template <typename T>
struct alpha<std::tuple<>, T>
{
enum { value = 1 };
};
template <typename T, typename U, typename Args...>
struct alpha<std::tuple<U, Args...>, T>
{
typedef typename Last<std::tuple<U, Args...>>::type LastType;
enum { value = alpha_tuple<LastType,T>::value };
};
Note that there is no last type for empty tuples, so I had to deal with them in a separate specialization.
If you like to find out whether a tuple as a specific last member, here's a type trait for that:
#include <type_traits>
#include <tuple>
template <typename ...Args> struct back;
template <typename T, typename ...Args> struct back<T, Args...>
{ typedef typename back<Args...>::type type; };
template <typename T> struct back<T>
{ typedef T type; };
template <typename...> struct tuple_has_last : public std::false_type {};
template <typename T, typename... Args> struct tuple_has_last<T, std::tuple<Args...>>
{
static const bool value = std::is_same<typename back<Args...>::type, T>::value;
};
Edit: Oh, I didn't see that Matthieu had already written the exact same thing. Never mind.