Using with variadic template - c++

I know that the following code compile:
template<class Type>
class Foo
{
using type = Type;
};
now, I'm trying to compile the following code:
template<class Type, class... OtherTypes>
class Foo
{
using type = Type;
// using types = OtherTypes;
// using... types = OtherTypes;
// using types... = OtherTypes;
// using types = OtherTypes...;
// using types... = OtherTypes...;
};
I tried all of the options of the code in comments, but none of them compile.
How can I fix it?

You cannot have a pack of types as a type in a class.
The closest you can get is roughly:
template<class...Ts> struct types_t { constexpr types_t(){}; };
template<class...Ts> constexpr types_t<Ts...> types{};
these are values and types that represent a package of types.
template<class Type, class... OtherTypes>
class Foo
{
using type=Type;
using types=types_t<OtherTypes...>;
};
then we can write helper functions that consume bundled-up types and use them elsewhere.
template<template<class...>class Z, class Types>
struct apply_types;
template<template<class...>class Z, class...Ts>
struct apply_types<Z, types_t<Ts...>> {
using type=Z<Ts...>;
};
template<template<class...>class Z, class Types>
using apply_types_t = typename apply_types<Z,Types>::type;
now apply_types< some_template, some_types_t > takes the types in the bundle and passes them to the template.

Let's assume that you want to use the pack as template argument. Then you can try the following approach.
#include <utility>
template <class... Types>
struct Foo {};
template <template <class...> class Template,
class... Types,
template <class...> class T>
Template<Types...> foo(const T<Types...> &);
template <template <class...> class Template, class T>
using Type = decltype(foo<Template>(std::declval<T>()));
int main() {
using T = Foo<int, int>;
// As template argument
using Tuple = Type<std::tuple, T>;
static_assert(std::is_same<Tuple, std::tuple<int, int> >::value, "");
return 0;
}

Related

Expand template type

Is there a way to convert each type of std::tuple into specific subtypes?
I have following code
struct Foo1
{
struct A{};
struct B{};
};
struct Foo2
{
struct A{};
struct B{};
};
using myTypes = std::tuple<Foo1, Foo2>;
Is there a way to convert myTypes into following type?
std::tuple<Foo1::A, Foo1::B, Foo2::A, Foo2::B>;
Order of types doesn't matter, but would be nice to have it like above.
If A/B name are fixed, you might do
template <typename... Ts>
using AB_Types = std::tuple<typename Ts::A..., typename Ts::B...>;
So AB_Types<Foo1, Foo2> is std::tuple<Foo1::A, Foo2::A, Foo1::B, Foo2::B>.
Having expected order would also be possible:
template <typename... Ts>
using AB_Types_ordered =
decltype(std::tuple_cat(std::tuple<typename Ts::A, typename Ts::B>{}...));
and if source is a tuple, just add extra layer
template <typename>
struct AB_impl;
template <typename... Ts>
struct AB_impl<std::tuple<Ts...>>
{
using type = AB_Types<Ts...>; // AB_Types_ordered<Ts...>
};
template <typename T>
using AB_Types_from_tuple = typename AB_impl<T>::type;
An alternative solution based on the Boost.Mp11 library:
template<class T>
using add_AB = std::tuple<typename T::A, typename T::B>;
template <typename Tuple>
using AB_Types_from_tuple =
boost::mp11::mp_flatten<boost::mp11::mp_transform<add_AB, Tuple>>;
static_assert(std::is_same_v<
AB_Types_from_tuple<myTypes>,
std::tuple<Foo1::A, Foo1::B, Foo2::A, Foo2::B>
>);

How to expand the content of an mpl::set as template parameters of a function template

I have a function template with a variadic number of template parameters:
template <typename T1, typename... Ts>
void doSomething()
{
...
}
Furthermore I have an mpl set defined as follows:
template <typename... Ts>
struct MyContainerCreator
{
using type = boost::mpl::set<Ts...>;
};
using MyContainer= MyContainerCreator<T1, T2>;
Now I want to write a function doSomethingForAll() that calls doSomething() with the types in the mpl set as template parameters. Something like:
void doSomethingForAll()
{
//pseudocode:
doSomething<expandTypesToTemplateParameters<MyContainer::type>>();
}
Is this possible?
Essentially you want a lifting function that maps a template<class...> class TT with a mpl::set<Ts...> to TT<Ts...>.
To do this generically, write a lifter with the help of Foldable:
template<template<class...> class TT>
struct lifter {
template<class Foldable>
struct apply {
template <class Left, class Current> struct one_stepper;
template<class... Ts, class Current>
struct one_stepper<TT<Ts...>, Current> {
using type = TT<Ts..., Current>;
};
using type = typename mpl::fold<Foldable, TT<>,
one_stepper<mpl::_1, mpl::_2>
>::type;
};
};
Then you can use lift mpl container in this way:
template<class... Ts> struct foo {};
using u = lifter<foo>::apply<mpl::vector<int, long>>::type;
using v = lifter<foo>::apply<mpl::set<int, long>>::type;
Then u is foo<int, long>, v is foo<int, long> or foo<long, int> depends on mpl implementation.
With this tool, your task can be done by:
template<class... Ts>
struct doSomething_helper {
static void do_() { doSomething<Ts...>(); }
};
void doSomethingForAll()
{
//pseudocode:
// doSomething<expandTypesToTemplateParameters<MyContainer::type>>();
lifter<doSomething_helper>::apply<typename MyContainer::type>::type::do_();
}

Having a template parameter depend on a parameter list

I have defined the class
template <typename... Ts> struct Bar {
using inner_type = /* whatever */;
};
Now, I need to define a templated class Foo whose template parameters are some parameter pack, and a value of type Bar::inner_type instantiated for that parameter pack. Unfortunately I can't seem to be able to do it. If I define it this way:
template <Bar<Ts...>::inner_type SomeValue, typename... Ts> struct Foo { };
the compiler doesn't recognize Ts when it's used, since it hasn't see the parameter pack yet; but if I define it this way:
template <typename... Ts, Bar<Ts...>::inner_type SomeValue> struct Foo { };
the compiler sneers at my attempt to use a parameter pack before other template parameters.
So how can I do this?
Note: In case it matters, this failed for me with GCC 4.9.3.
You can partially specialize your struct:
template<typename...>
struct Bar { using inner_type = int; };
template <typename T, typename T::inner_type T>
struct Foo;
template <typename... Ts, typename Bar<Ts...>::inner_type SomeValue>
struct Foo<Bar<Ts...>, SomeValue> { };
int main() {
Foo<Bar<int>, 3> foo;
}
This way Ts parameter pack is deduced and Foo expects the second template parameter to be of type Bar<Ts...>::inner_type.
The best thing I could come up with:
template <class Inner, Inner Val, class... Args>
struct Foo {
static_assert(std::is_same<Inner, typename Bar<Args...>::inner_type>::value, "Wrong type");
};
You need to explicitly name the type.
does this solve the issue?
#include <type_traits>
using namespace std;
template <typename... Ts> class Bar {
public:
using inner_type = int;
};
template <typename... Ts> class Foo {
using bar_inner_type = typename Bar<Ts...>::inner_type;
static_assert(is_same<int, bar_inner_type>::value,"");
};
If I understood your problem correctly, you can do something like this:
template <typename... Ts> struct Bar {
using inner_type = /* whatever */;
};
template <typename... Ts> struct Foo {
using inner_type = typename Bar<Ts...>::inner_type;
};

Bind metafunction: accept both types and template template parameters (accept anything)

I'm trying to write a Bind metaprogramming template helper metafunction that binds a template parameter to something.
I have a working implementation for simple template metafunctions:
template<typename T0, typename T1>
struct MakePair
{
using type = std::pair<T0, T1>;
};
template<template<typename...> class TF, typename... Ts>
struct Bind
{
template<typename... TArgs>
using type = TF<Ts..., TArgs...>;
};
using PairWithInt = typename Bind<MakePair, int>::type;
static_assert(std::is_same<PairWithInt<float>, MakePair<int, float>>{}, "");
But what if MakePair's template arguments were template templates? Or simple numerical values?
template<template<typename> class T0, template<typename> class T1>
struct MakePair0
{
using type = /*...*/;
};
template<template<typename...> class TF, template<typename> class... Ts>
struct Bind0 { /*...*/ }
// ...
template<int T0, int T1>
struct MakePair1
{
using type = /*...*/;
};
template<template<int...> class TF, int... Ts>
struct Bind1 { /*...*/ }
A lot of unnecessary repetition. It gets unmanageable if template arguments are mixed between types, template templates, and integral constants.
Is something like the following piece of code possible?
template<template<ANYTHING...> class TF, ANYTHING... Ts>
struct BindAnything
{
template<ANYTHING... TArgs>
using type = TF<Ts..., TArgs...>;
};
ANYTHING would accept types, template templates, template template templates, integral values, etc...
When I'm doing serious metaprogramming, I turn everything into types.
template<class T>struct tag{using type=T;};
template<class Tag>using type_t=typename Tag::type;
template<template<class...>class> struct Z {};
template<class Z, class...Ts>
struct apply {};
template<template<class...>class z, class...ts>
struct apply< Z<z>, ts... >:
tag< z<ts...> >
{};
template<class Z, class...Ts>
using apply_t = type_t< apply<Z, Ts...> >;
now we pass template<?> foo around as Z<foo>, and it is now a type.
Similar things can be done for constants, using std::integral_constant<T, t> (and easier to use aliases of same), or template<class T, T* p> struct pointer_constant {};, by turning them into types.
Once everything is a type, your metaprogramming becomes more uniform. Templates just become a kind of type on which apply_t does things to.
There is no way in C++ to have a template argument that can be a type, a value or a template. So this is about the best you can get.
templates not written for the above pattern need to be wrapped up, and their arguments "lifted" to being types. As an example:
template<class T, class t>
using number_constant = std::integral_constant< T, t{} >;
using number_constant_z = Z<number_constant>;
has had its arguments "lifted" from values to types, and then it has been wrapped with a Z to turn itself into a type.
Bind now reads:
template<class z, class... Ts>
struct Bind {
template<class... More>
using type_base = apply_t< z, Ts..., More... >;
using type = Z<type_base>;
};
template<class Z, class...Ts>
using Bind_t = type_t<Bind<Z,Ts...>>; // strip ::type
using Bind_z = Z<Bind_t>; // quote into a Z<?>
and Bind_z is a type wrapping a template that returns a wrapped template, and takes a type that wraps a template as its first argument.
To use it:
template<class...>struct types{using type=types;};
using types_z=Z<types>;
template<class...Ts>
using prefix =apply_t< Bind_z, types_z, Ts... >;
using prefix_z = Z<prefix>;
prefix_z takes a set of types, and generates a factory of types<?...> that will contain the prefix Ts... first.
apply_t< apply_t< prefix_z, int, double, char >, std::string >
is
types< int, double, char, std::string >
live example.
There is another fun approach: do metaprogramming in functions:
template<template<class...>class z, class...Ts>
constexpr auto apply_f( Z<z>, tag<Ts>... )
-> tag<z<Ts...>> { return {}; }
here, types are represented by values of type tag<t>, templates a Z<z> and values as std::integral_constant<?>.
These two:
template<class T>
constexpr tag<T> Tag = {};
template<template<class...>class z>
constexpr Z<z> Zag = {};
give you ways to get values that represent types and templates respectively.
#define TYPEOF(...) type_t<decltype(__VA_ARGS__)>
is a macro that moves from an instance of a tag to type type in the tag, and Tag<?> moves from a type to an instance of a tag.
TYPEOF( apply_f( apply_f( Zag<prefix>, Tag<int>, Tag<double>, Tag<char> ), Tag<std::string> ) )
is
apply_t< apply_t< prefix_z, int, double, char >, std::string >
strange, but can be interesting.
I think you're looking for quote and map here. First, you want something that given a "metafunction class" and a sequence of arguments gives you a new type:
template <typename MCls, typename... Args>
using map = typename MCls::template apply<Args...>;
As the implementation here suggests, a metafunction class is one that has an member alias template named apply.
To turn a class template into a metafunction class, we introduce quote:
template <template <typename...> class C>
struct quote {
template <typename... Args>
using apply = C<Args...>;
};
The above is sufficient to do something like:
using T = map<quote<std::tuple>, int, char, double>;
to yield the type:
std::tuple<int, char, double>
In your example, we could write:
using P = map<quote<MakePair>, int, char>::type; // std::pair<int, char>
but I would instead prefer to make MakePair a metafunction class directly:
struct MakePair2 {
template <typename T, typename U>
using apply = std::pair<T, U>;
};
using P = map<MakePair2, int, char>; // also std::pair<int, char>
that avoids the extra ::type.
Consistently use the concepts of metafunction (a type with a member typedef named type, e.g. map) and a metafunction class (a type with a member template alias named apply, e.g. quote) and use only those concepts throughout your metaprogramming code. Values and class templates are second-class citizens.

Get variadic template variadic template parameter variadic parameters

Yes.
Let's say I have a simple variadic struct that holds a typedef:
template<typename... TArgs> struct TupleTypeHolder {
using TupleType = std::tuple<TArgs*...>;
};
I want to pass TupleTypeHolder<something> as a template parameter to another class, and get that typedef.
All of my tries do not compile.
// None of these is valid
template<template<typename...> class TTupleTypeHolder> struct TupleMaker {
using MyTupleType = TTupleTypeHolder::TupleType; // Not valid
using MyTupleType = typename TTupleTypeHolder::TupleType; // Not valid
};
template<template<typename... A> class TTupleTypeHolder> struct TupleMaker2 {
// A is not a valid name here
using MyTupleType = TTupleTypeHolder<A...>::TupleType; // Not valid
using MyTupleType = typename TTupleTypeHolder<A...>::TupleType; // Not valid
};
Is there a way to use the variadic template parameters (in this case, TupleTypeHolder's TArgs...) of a variadic template class from a class that uses the aforementioned class as a template variadic template parameter?
Usage example:
template<typename... TArgs> struct TupleTypeHolder {
using TupleType = std::tuple<TArgs*...>;
};
template<typename... TArgs> static int getSomeValue() { ... }
template<??? T1, ??? T2> class TupleMaker
{
std::pair<int, int> someValues;
using TupleType1 = T1::TupleType;
using TupleType2 = T2::TupleType;
TupleMaker() : someValues{getSomeValue<T1's TArgs...>(),
getSomeValue<T2's TArgs...>()} { }
};
class MyTupleMaker : TupleMaker<TupleTypeHolder<int, char>,
TupleTypeHolder<int, float>>
{ };
MyTupleMaker::TupleType1 tuple1{new int(1), new char('a')};
MyTupleMaker::TupleType2 tuple1{new int(35), new float(12.f)};
Working usage example:
#include <tuple>
template<typename... TArgs> struct TupleTypeHolder {
using TupleType = std::tuple<TArgs*...>;
};
template<typename... TArgs> static int getSomeValue() { return 42; }
// primary template:
template<class T1, class T2>
struct TupleMaker;
// partial specialization:
template<template<class...> class TT1, template<class...> class TT2,
class... T1, class... T2>
struct TupleMaker < TT1<T1...>, TT2<T2...> >
{
std::pair<int, int> someValues;
using TupleType1 = typename TT1<T1...>::TupleType;
using TupleType2 = typename TT2<T2...>::TupleType;
TupleMaker() : someValues{getSomeValue<T1...>(),
getSomeValue<T2...>()} { }
};
struct MyTupleMaker : TupleMaker<TupleTypeHolder<int, char>,
TupleTypeHolder<int, float>>
{ };
MyTupleMaker::TupleType1 tuple1{new int(1), new char('a')};
MyTupleMaker::TupleType2 tuple2{new int(35), new float(12.f)};
int main() {}
The primary template takes two types, as you're passing types. TupleTypeHolder<int, char> is a type, a specialization of a template, not a template itself. Template template-parameters however take templates as arguments (not types), such as:
template<template<class...> class Foo>
struct Bar
{
using type = Foo<int, double, char>;
};
Bar< std::tuple > b; // note: no template arguments for `std::tuple`!
With partial specialization, you can split a template specialization into the template and the parameters, that's how the above works.
Template-template parameters are not really type parameters, are parameters to specify a template. That means what you pass through a template-template parameter is not a type, is a template:
template<template<typename> class TPARAM>
struct give_me_a_template
{
using param = TPARAM; //Error TPARAM is not a type, is a template.
using param_bool = TPARAM<bool>; //OK, thats a type
};
As you can see, the first alias is invalid, because TPARAM is not a type, is a template. But the second is a type (Is an instance of the template).
That said, examine your problem: What you called TupleTypeHolder could be viewed as a variadic-template typelist. So your goal is to make tuples of the types specified with typelists, right?
You can use partial specialization to extract the content of a typelist:
template<typename TupleTypeHolder>
struct tuple_maker;
template<typename... Ts>
struct tuple_maker<TupleTypeHolder<Ts...>>
{
using tuple_type = std::tuple<Ts...>;
};
An example of its usage could be:
using my_types = TupleTypeHolder<int,int,int>;
using my_tuple_type = typename tuple_maker<my_types>::tuple_type;
Of course this is not the exactly solution to your implementation, you need to extend the concept to multiple typelists (As your question showed). What I have provided is the guide to understand the problem and its solution.