Template class specialization with a template template argument - c++

#include <tuple>
#include <iomanip>
template <typename T, typename ...L>
struct foo{};
template <typename T>
struct bar{
using toto = T;
};
template <template<typename T, typename ...L> class F>
struct bar<F>{
using toto = T
};
int main(){
bar<foo<int,char,char>> a;
}
I want to specialize bar when the argument is a class that has at least one template argument <typename T, typename ...L>
I tried :
template <template<typename T, typename ...L> class F>
struct bar<F<T,L...>>{
using toto = T
};
and
template <template<typename , typename ...> class F, typename T, typename ...L>
struct bar<F<T,L...>>{
using toto = T
};
which may have made sense, but I couldn't get it right

You forgot a lot of stuff on your sample, syntactically speaking
template <typename T, typename... L>
struct foo{};
template <typename T>
struct bar {
using toto = T; // Semicolon missing
};
template <template<typename, typename...> class F, typename T, typename... L>
struct bar<F<T,L...>> { // Wrong pack expansion
using toto = T;
};
int main() { // () missing
bar< foo<int,char,char> > a; // Pass the parameters to foo since you're
// partially specializing bar to just do that
}
Example on ideone

Your ideone code just has a bunch of typographical errors:
struct bar<F<T,...L>>{
//should be
struct bar<F<T,L...>>{
//missing brackets
int main{
//missing semicolon
using toto = T
bar<foo, int,char,char> a;
//should be
bar<foo<int,char,char>> a;

There's a few syntatic issues here.
bar is a template that takes one type argument. So any partial or explicit specialization of bar must also take one type argument.
template <template<typename T, typename ...L> class F>
struct bar<F> {
Here, the T and L names are irrelevent, and really you're specializating on a template template. That doesn't match. You'd have to specialize on a specific instantiation of F:
template <template<typename , typename ...> class F,
typename T, typename... L>
struct bar<F<T, L...>> {
You're missing a semicolon:
using toto = T;
^^
Your declaration of main is missing parentheses:
int main() {
bar<foo<int,char,char>> a;
}

Related

How can a recover the non-typename template argument from each type in a variadic pack of template classes?

I am trying to recover the non-typename template argument from a variadic pack of template classes (where each class has one non-type argument) so that I can use them as an integer sequence in another type.
The code below shows what I have. The integer sequence / member sequence is cribbed from tuple.
template<std::size_t... Integers>
struct size_t_sequence {
using type = size_t_sequence<Integers...>;
};
template <std::size_t, typename>
struct push_size_t_sequence;
template <std::size_t I, std::size_t... Integers>
struct push_size_t_sequence<I, size_t_sequence<Integers...>>
: size_t_sequence<I, Integers...> {};
template <std::size_t N, std::size_t Head, std::size_t... Tail>
struct make_size_t_sequence
: push_size_t_sequence<Head, typename make_size_t_sequence<N - 1, Tail...>::type>::type {};
template<std::size_t I, std::size_t... OneLeft>
struct make_size_t_sequence <2, I, OneLeft...> :
push_size_t_sequence<I, size_t_sequence<OneLeft...>>::type {};
template<typename... Members>
struct member_sequence {
using type = member_sequence<Members...>;
};
template <typename, typename>
struct push_member_sequence;
template <typename M, typename... Members>
struct push_member_sequence<M, member_sequence<Members...>>
: member_sequence<M, Members...> {};
template <std::size_t N, typename Head, typename... Tail>
struct make_member_sequence
: push_member_sequence<Head, typename make_member_sequence<N - 1, Tail...>::type>::type {};
template<typename M, typename... OneLeft>
struct make_member_sequence <2, M, OneLeft...> :
push_member_sequence<M, member_sequence<OneLeft...>>::type {};
template<typename>
struct unpack_sequence_impl;
template<template<std::size_t> class... T, std::size_t... DimensionSizes>
struct unpack_sequence_impl<member_sequence<T<DimensionSizes>...>> {
using member_types = member_sequence<T<DimensionSizes>...>;
using size_types = size_t_sequence<DimensionSizes...>;
};
template<typename... Members>
struct unpack_sequence :
unpack_sequence_impl<make_member_sequence<sizeof...(Members), Members...>> {
using base_t = unpack_sequence_impl<make_member_sequence<sizeof...(Members), Members...>>;
using member_types = typename base_t::member_types;
using size_types = typename base_t::size_types;
};
template<std::size_t N>
class example {
int s = N;
};
int main()
{
auto mem_sequence = make_member_sequence<3, example<3>, example<2>, example<1>>::type();
auto integer_sequence = make_size_t_sequence<3, 3,2,1>::type();
auto un_mem_sequence = unpack_sequence<example<3>, example<2>, example<1>>::member_types();
auto un_size_sequence = unpack_sequence<example<3>, example<2>, example<1>>::size_types();
}
The types of mem_sequence and integer_sequence are member_sequence<example<3>,example<2>,example<1>> and size_t_sequence<3,2,1>. The types of un_mem_sequence and un_size_sequence should be the same.
How can I accomplish this?
Thank you for your help!
Tim
Edit:
For clarification what I am trying to accomplish is recovering the template arguments from one template class to use them in another. Below are three template classes: MyObject, MyTuple, MyPack. MyTuple takes MyObject objects as its template parameters. I want to recover the MyObject template parameters to use as the template argument for a MyPack object.
template<int N>
MyObject;
template<int... Ns>
MyPack;
template<typename... MyObjects>
MyTuple {};
int main() {
MyTuple<MyObject<1>,MyObject<2>,MyObject<3>> a;
MyPack<1,2,3> b;
}
So I want to pull the arguments from the MyObject's in MyTuple to be used to create a MyPack.
Edit 2:
Second clarification: MyTuple does not just take MyObject types but any type that has one template parameter of int.
template<int N>
MyObject;
template<int N>
MyObject2;
template<int... Ns>
MyPack;
template<typename... MyObjects>
MyTuple {};
int main() {
MyTuple<MyObject<1>,MyObject<2>,MyObject2<1>,MyObject2<2>> a;
MyPack<1,2,1,2> b;
}
template <typename>
struct MakeMyPack;
template <int... Ns>
struct MakeMyPack<MyTuple<MyObject<Ns>...>>
{
using type = MyPack<Ns...>;
};
DEMO
For clarification what I am trying to accomplish is recovering the template arguments from one template class to use them in another. Below are three template classes: MyObject, MyTuple, MyPack. MyTuple takes MyObject objects as its template parameters. I want to recover the MyObject template parameters to use as the template argument for a MyPack object.
Just for fun, I propose a solution more generic, with a double variadic template (template-template and value template), not bounded to MyTuple and MyObject
template <typename>
struct getMyPack;
template <template <typename...> class C,
template <int> class ... Cs, int ... Is>
struct getMyPack<C<Cs<Is>...>>
{ using type = MyPack<Is...>; };
The following is a full compiling example
#include <type_traits>
template <int>
struct MyObject1
{ };
template <int>
struct MyObject2
{ };
template <int...>
struct MyPack
{ };
template <typename...>
struct MyTuple
{ };
template <typename>
struct getMyPack;
template <template <typename...> class C,
template <int> class ... Cs, int ... Is>
struct getMyPack<C<Cs<Is>...>>
{ using type = MyPack<Is...>; };
int main ()
{
using T0 = MyTuple<MyObject1<1>, MyObject2<2>, MyObject1<3>>;
using T1 = MyPack<1, 2, 3>;
using T2 = typename getMyPack<T0>::type;
static_assert( std::is_same<T1, T2>::value, "!" );
}
-- EDIT --
Second clarification: MyTuple does not just take MyObject types but any type that has one template parameter of int.
As I was suspecting.
My solution is unboud to MyObject so should works.
Preceding example modified to show it.

template template argument deduction with sfinae

The template template argument can't be deduced for both foo and foo2.
If I remove the sfinae part, the template template argument was successfully deduced for both foo and foo2.
How to fix either the span class or foo and foo2?
Thanks.
Test (also at godbolt.org)
#include <type_traits>
template<typename T>
using enable_if_t_const = typename std::enable_if<
std::is_const<T>::value
>::type;
template<typename T, typename=void> class span;
template<typename T>
class span<T, enable_if_t_const<T>> {
public:
explicit span(const T* const data) {}
};
template <typename T, template<typename> class S>
void foo() {}
template <typename T, template<typename> class S>
void foo2(S<T>& s) {}
int main() {
int arr[] = {1};
span<const int> s(arr);
foo<const int, span>();
foo2(s);
return 0;
}
This is because, although you have a default template parameter, span isn't a template <typename> class S. It's a template <typename, typename> class S.
The easiest fix is to change it to
template <typename T, template<typename...> class S>
void foo2(S<T>& s) {}
So that you can accept any S that takes any number of types (although we only use it with one).
Demo

Retrieve the template a type is instantiated from

How can I retrieve the template a type was originally instantiated from?
I'd like to do the following:
struct Baz{};
struct Bar{};
template <typename T>
struct Foo {};
using SomeType = Foo<Bar>;
template <typename T>
using Template = get_template<SomeType>::template type<T>;
static_assert(std::is_same<Foo<Baz>, Template<Baz>>::value, "");
I know I can achieve this through partial specialization, but this forces me to specialize get_template for every template I want to use it with:
template <typename T>
struct get_template;
template <typename T>
struct get_template<Foo<T>>
{
template <typename X>
using type = Foo<X>;
};
live example
Is there a way around this limitation?
You could do something like that, using a template template parameter (should work for templates with any number of type arguments):
template <typename T>
struct get_template;
template <template <class...> class Y, typename... Args>
struct get_template<Y<Args...>> {
template <typename... Others>
using type = Y<Others...>;
};
Then to get the template:
template <typename T>
using Template = typename get_template<SomeType>::type<T>;
As mentioned by #Yakk in the comment, the above only works for template that only have type arguments. You can specialize for template with specific pattern of type and non-type arguments, e.g.:
// Note: You need the first size_t to avoid ambiguity with the first specialization
template <template <class, size_t, size_t...> class Y, typename A, size_t... Sizes>
struct get_template<Y<A, Sizes...>> {
template <class U, size_t... OSizes>
using type = Y<U, OSizes...>;
};
...but you will not be able to specialize it for arbitrary templates.
DEMO (with Foo and std::pair):
#include <type_traits>
#include <map>
struct Bar{};
template <typename T>
struct Foo {};
using SomeType = Foo<Bar>;
template <typename T>
struct get_template;
template <template <class...> class Y, typename... Args>
struct get_template<Y<Args...>> {
template <typename... Others>
using type = Y<Others...>;
};
template <typename T>
using Template = typename get_template<SomeType>::type<T>;
static_assert(std::is_same<SomeType, Template<Bar>>::value, "");
static_assert(std::is_same<Foo<int>, Template<int>>::value, "");
using PairIntInt = std::pair<int, int>;
using PairIntDouble = std::pair<int, double>;
template <typename U1, typename U2>
using HopeItIsPair =
typename get_template<PairIntDouble>::type<U1, U2>;
static_assert(std::is_same<PairIntDouble, HopeItIsPair<int, double>>::value, "");
static_assert(std::is_same<PairIntInt, HopeItIsPair<int, int>>::value, "");
Not sure I got the question. Would this work?
#include<type_traits>
#include<utility>
template<typename V, template<typename> class T, typename U>
auto get_template(T<U>) { return T<V>{}; }
struct Baz{};
struct Bar{};
template <typename T>
struct Foo {};
using SomeType = Foo<Bar>;
template <typename T>
using Template = decltype(get_template<T>(SomeType{}));
int main() {
static_assert(std::is_same<Foo<Baz>, Template<Baz>>::value, "");
}
You can generalize even more (SomeType could be a template parameter of Template, as an example), but it gives an idea of what the way is.

How to create template class with member function pointer?

Is anyone knows how to declare generalized template form for the next template specialization:
template <template <class> class Container,
class Value,
class Return,
Return (Container<Value>::*Apply)(const Value &)>
class Example<Container<Value>, Apply>
{
};
Apply must be a pointer to a member function whoes signature is unknown in template declaration.
Do you mean something like this?
template<typename T, typename F>
struct S;
template<template <typename...> class C, typename R, typename... A>
struct S<C<A...>, R(A...)> {
using Apply = R(C<A...>::*)(A...);
// ...
};
As an example:
template<typename... U>
struct T {
int f(int, char) { return 42; }
};
template<typename T, typename F>
struct S;
template<template <typename...> class C, typename R, typename... A>
struct S<C<A...>, R(A...)> {
using Apply = R(C<A...>::*)(A...);
// ...
};
int main() {
S<T<int, char>, int(int, char)>::Apply apply = &T<int, char>::f;
}
Pretty ugly, indeed, but that's what the OP (maybe) asked.

Alias for nested variadic templated struct in VC++2013

For a templated nested struct I can define aliases like the following:
template<typename T>struct Struct
{
Struct(T value){}
template<typename T1> struct Nested
{
Nested(T1 value){}
};
};
template<typename T, typename T1>using NameT = struct Struct<T>::Nested<T1>;
using Name = NameT<int, double>; // Alias for a certain instance
Can someone please give me a hint how to declare an alias for the variadic one?
template<typename... T>struct Struct
{
Struct(T... value){}
template<typename T1> struct Nested
{
Nested(T1 value){}
};
};
template<typename... T, typename T1>using NameT = struct Struct<T...>::Nested<T1>; // This seems ok
using Name = NameT<int, double>; // error C976: 'Name': too few template arguments
This will work:
template<typename T1, typename... T>using NameT =
typename Struct<T...>::template Nested<T1>; // This seems ok
^^^^^^^^ ^^^^^^^^
using Name = NameT<int, double>;
Mind though that parameter pack must be at the end so the definition is slightly changed.
LIVE DEMO
Change your code to :
template<typename... T>
struct Struct
{
Struct(T... value){}
template<typename T1>
struct Nested
{
Nested(T1 value){}
};
};
template<typename T1, typename... T>
using NameT = typename Struct<T...>::template Nested<T1>;
using Name = NameT<int, double>;
int main()
{
Name f(1);
}
The variadic template has to be at the end of the template argument list.
#include <type_traits>
#include <tuple>
#include <utility>
template <typename... T>
struct Struct
{
Struct(T... value) {}
template <typename T1>
struct Nested
{
Nested(T1 value) {}
};
};
template <typename, typename>
struct split;
template <typename T, std::size_t... Is>
struct split<T, std::index_sequence<Is...>>
{
using type = typename Struct<typename std::tuple_element<Is, T>::type...>::template Nested<typename std::tuple_element<sizeof...(Is), T>::type>;
};
template <typename... T>
using NameT = typename split<std::tuple<T...>, std::make_index_sequence<sizeof...(T)-1>>::type;
int main()
{
static_assert(std::is_same<NameT<int, double, char>
, Struct<int, double>::Nested<char>>{}, "!");
}
DEMO