So someone asked a question then deleted it, but I found it an interesting challenge.
They had this weird type, and had run into the problem that Concat and Range were castable to Sequences but were not Sequences (for obvious reasons)
template<unsigned... Is> struct Sequence{};
template<typename... Is> struct Concat{};
template<unsigned... Is> struct Concat<Sequence<Is...>> : public Sequence<Is...> {};
template<unsigned... Is, unsigned... Js, typename... Rest>
struct Concat<Sequence<Is...>, Sequence<Js...>, Rest...>
: Concat<Sequence<Is..., Js...>, Rest...> {};
So I figured it'd be an interesting challenge to do this right, making everything the same type.
So my first pass at Concat ran into unexpected problems...How on earth does one extract the template parameters for the Sequences? I ended up using a function below:
template <unsigned... Is,unsigned... Js>
constexpr auto concat_results(Sequence<Is...>,Sequence<Js...>) -> Sequence<Is...,Js...>
{
return Sequence<Is...,Js...>();
}
template <typename seq1,typename seq2>
using Concat = decltype(concat_results(seq1(),seq2()));
Bizarrely, I think that's the shortest way to declare it, though it is a little weird. However, the concat_results could be useful (on references of the sequences) and the type can be useful in other places. Anyone have a better way to do that?
Next up was range, which I did with a recursive struct helper:
template <unsigned N>
struct nrange_helper
{
using Range = Concat<typename nrange_helper<N-1>::Range,Sequence<N>>;
};
template <>
struct nrange_helper<0>
{
using Range = Sequence<0>;
};
template <unsigned N>
using Range = typename nrange_helper<N>::Range;
Again, its weird, but it works...but I feel like there should be a better way. Anyone got one?
Note: please check out std::integer_sequence, its specialization index_sequence and its make_index_sequence and index_sequence_for facilities.
If you want to declare them yourself, you generally would use inner types:
template <typename, typename> struct Concat;
template <unsigned... Is, unsigned... Js>
struct Concat<Sequence<Is...>,Sequence<Js...>> {
using type = Sequence<Is..., Js...>;
};
template <unsigned N>
struct Range {
using type = typename Concat<typename Range<N-1>::type, Sequence<N>>::type;
};
template <> struct Range<0> { using type = Sequence<0>; };
Which can of course be combined to template aliases for a shorter final form:
template <typename, typename> struct ConcatH;
template <unsigned... Is, unsigned... Js>
struct ConcatH<Sequence<Is...>,Sequence<Js...>> {
using type = Sequence<Is..., Js...>;
};
template <typename L, typename R>
using Concat = typename ConcatH<L, R>::type;
and:
template <unsigned N>
struct RangeH {
using type = Concat<typename RangeH<N-1>::type, Sequence<N>>;
};
template <> struct RangeH<0> { using type = Sequence<0>; };
template <unsigned N>
using Range = typename RangeH<N>::type;
However, indeed, this is slightly verbose. Still, there is little cruft here:
baseline declaration
specialization (to end recursion or limit to "interesting" types)
alias declaration
on the other hand it's only written once.
Related
template<int Degree> using curve = // some definition
template<int MaxDegree> using curve_variant = std::variant<curve<1>, curve<2>, .. curve<MaxDegree>>;
In the above example, the number of parameters passed to std::variant's template would change depending on curve_variant's parameters:
For example, curve_variant<4> would resolve to std::variant<curve<1>, curve<2>, curve<3>, curve<4>>.
Since MaxDegree is known at compile time, this feels possible. But I also don't have a clue how to begin implementing this.
With std::integer_sequence helper, you might do:
template <typename Seq> struct curve_variant_impl;
template <int ... Is>
struct curve_variant_impl<std::integer_sequence<int, Is...>>
{
using type = std::variant<curve<1 + Is>...>;
};
template <int MaxDegree>
using curve_variant = typename curve_variant_impl<std::make_integer_sequence<int, MaxDegree>>::type;
As the other answers show std::integer_sequence is a nice tool. Suppose we didn't have it.
The following is only to illustrate what code we would have to write if we didn't have std::integer_sequence. As a matter of fact, there is no reason to write it this way, if you do not have C++14, you can reimplement is easily.
#include <variant>
#include <type_traits>
template<int Degree> struct curve{};
// helper to add a type to a variant
template <typename A,typename... others>
struct merge_variants {
using type = std::variant<others...,A>;
};
template <typename A,typename... others>
struct merge_variants<A,std::variant<others...>> : merge_variants<A,others...> {};
// the recursion:
template <int MaxDegree>
struct Foo {
using type = typename merge_variants< curve<MaxDegree>,typename Foo<MaxDegree-1>::type >::type;
};
// the base case:
template <>
struct Foo<1> {
using type = std::variant< curve<1> >;
};
int main() {
static_assert(std::is_same<std::variant<curve<1>,curve<2>,curve<3>> , Foo<3>::type >::value);
}
Recursion is rather expensive, to instantiate Foo<N> (sorry for the name) N other types have to be instantiated, even though we never asked for them. std::integer_sequence can avoid the recursion completely.
#include <utility>
#include <variant>
template<int Degree>
struct curve{};
template<typename index_seq>
struct curve_variant_impl;
template<int...indices>
// Start binding indices from 1, not zero
struct curve_variant_impl<std::integer_sequence<int,0,indices...>>{
using type = std::variant<curve<indices>...>;
};
template<int MaxDegree>
//make_integer_sequence makes [0,MaxDegree), we want [1,MaxDegree]
using curve_variant = typename curve_variant_impl<std::make_integer_sequence<int,MaxDegree+1>>::type;
int main() {
static_assert(std::is_same_v<curve_variant<4>,std::variant<curve<1>, curve<2>, curve<3>, curve<4>>>);
}
The above works only with non-negative values, so you might as well use std::size_t which is natural type for indices.
Just to go with the other answers, if you have C++11 but not C++14, you can emulate std::integer_sequence with a clever trick:
template <int...>
struct seq { };
template <int N, int... S>
struct gens : gens<N - 1, N - 1, S...> { };
template <int... S>
struct gens<0, S...> {
typedef seq<S...> type;
};
With this pattern, gens operates like integer_sequence does. gens<N>::type is seq<1, 2, 3, ..., N>. Thus, you use it just the same
template <typename T>
struct make_curve_variant_impl
template <int... N>
struct make_curve_variant_impl<seq<N...>>
{
typef std::variant<curve<N+1>...> type;
};
template <typename N>
struct make_curve_variant
{
typedef make_curve_variant_impl<typename gens<N>::type>:: type;
};
Given a parameter pack with variadic arguments, how can one find the number of unique values in the pack. I am looking for something along the lines of
no_of_uniques<0,1,2,1,2,2>::value // should return 3
My rudimentary implementation looks something this
template <size_t ... all>
struct no_of_uniques;
// this specialisation exceeds -ftemplate-depth as it has no terminating condition
template <size_t one, size_t ... all>
struct no_of_uniques<one,all...> {
static const size_t value = no_of_uniques<one,all...>::value;
};
template <size_t one, size_t two, size_t three>
struct no_of_uniques<one,two,three> {
static const size_t value = (one==two && one==three && two==three) ? 1:
(one!=two && two==three) ? 2:
(one==two && one!=three) ? 2:
(one==three && two!=three) ? 2: 3;
};
template <size_t one, size_t two>
struct no_of_uniques<one,two> {
static const size_t value = one==two ? 1: 2;
};
template <size_t one>
struct no_of_uniques<one> {
static const size_t value = 1;
};
Here, I have specialised for up to three arguments but understandably the code grows exponentially with the number of arguments. I would like to have a meta solution for this using solely STL and no third party libraries like Boost.MPL.
A similar question albeit in the context of checking unique types, rather than finding number of unique values of parameter pack can be found here:
Check variadic templates parameters for uniqueness
In the process of finding the number of unique values of a parameter pack, we might need to sort the pack first, and an excellent implementation of that is provided in this other question
Quick sort at compilation time using C++11 variadic templates
Here's a simple O(n^2) way to do it
template <size_t...>
struct is_unique : std::integral_constant<bool, true> {};
template <size_t T, size_t U, size_t... VV>
struct is_unique<T, U, VV...> : std::integral_constant<bool, T != U && is_unique<T, VV...>::value> {};
template <size_t...>
struct no_unique : std::integral_constant<size_t, 0> {};
template <size_t T, size_t... UU>
struct no_unique<T, UU...> : std::integral_constant<size_t, is_unique<T, UU...>::value + no_unique<UU...>::value> {};
So using your example:
no_unique<0, 1, 2, 1, 2, 2>::value; // gives 3
Most of this is machinery I already wrote for a different question, stripped of the "counting" part.
A pack with a sizeof shortcut:
template<class... Ts> struct pack {
static constexpr size_t size = sizeof...(Ts);
};
Add a type to a pack of types, but only if it doesn't exist already:
template<class T, class PT> struct do_push;
template<class T, class...Ts>
struct do_push<T, pack<Ts...>>{
using type = std::conditional_t<std::disjunction_v<std::is_same<Ts, T>...>,
pack<Ts...>,
pack<T, Ts...>
>;
};
template<class T, class PT> using push = typename do_push<T, PT>::type;
Now make a pack of unique types:
template<class P, class PT = pack<> >
struct unique_types_imp { using type = PT; };
template<class PT, class T, class... Ts>
struct unique_types_imp <pack<T, Ts...>, PT>
: unique_types_imp <pack<Ts...>, push<T, PT>> {};
template<class P>
using unique_types = typename unique_types_imp<P>::type;
Finally:
template<size_t S>
using size_constant = std::integral_constant<size_t, S>;
template<size_t... all>
struct no_of_uniques{
static constexpr size_t value = unique_types<pack<size_constant<all>...>>::size;
};
Using Boost.Mp11, this is a short one-liner (as always):
template <size_t... Ns>
using no_of_uniques = mp_size<mp_unique<mp_list<mp_size_t<Ns>...>>>;
Following the same logic as described below. We lift the values into types, put them in a type list, get the unique types out of that, and then get the length.
I'll generalize to types - since metaprogramming works better in types. The algorithm for counting uniqueness is an empty argument list has 0 unique types, and a non-empty list has 1 unique type + the number of unique types in the tail of that list after having removed the initial type.
In fact, let's generalize further than that - let's write a metafunction that takes a list of types and returns the unique types in it. Once you have the unique types, it's easy to count them.
template <class... > struct typelist { };
template <class >
struct uniq;
template <class TL>
using uniq_t = typename uniq<TL>::type;
template <>
struct uniq<typelist<>> {
using type = typelist<>;
};
template <class T, class... Ts>
struct uniq<typelist<T, Ts...>>
: concat<typelist<T>, uniq_t<filter_out_t<T, typelist<Ts...>>>>
{ };
Now we just need to fill in concat and filter_out_t. The latter is basically a glorified concat anyway:
template <class... > struct concat;
template <> struct concat<> { using type = typelist<>; };
template <class... Ts>
struct concat<typelist<Ts...>> {
using type = typelist<Ts...>;
};
template <class... Ts, class... Us, class... Args>
struct concat<typelist<Ts...>, typelist<Us...>, Args...>
: concat<typelist<Ts..., Us...>, Args...>
{ };
template <class T, class TL>
struct filter_out;
template <class T, class TL>
using filter_out_t = typename filter_out<T, TL>::type;
template <class T, class... Ts>
struct filter_out<T, typelist<Ts...>>
: concat<
std::conditional_t<std::is_same<T, Ts>::value, typelist<>, typelist<Ts>>...
>
{ };
Now, given a list of types, we can figure out the unique ones. To backport to the original problem, we just need a a size metafunction:
template <size_t N>
using size_t_ = std::integral_constant<size_t, N>;
template <class > struct length;
template <class T> using length_t = typename length<T>::type;
template <class... Ts>
struct length<typelist<Ts...>>
: size_t_<sizeof...(Ts)>
{ };
And then wrap everything up in one alias:
template <size_t... Ns>
using no_of_uniques = length_t<uniq_t<typelist<size_t_<Ns>...>>>;
I guess my whole problem is well described in the title. I am trying to create a variadic class template (in C++11, C++14 or C++1z).
template<typename ...Types> struct MyVariadicTemplate {};
and make sure that the list of types in any instantiation of MyVariadicTemplate is injective, so if I, for instance, call the following piece of code:
MyVariadicTemplate<int, double, int> x;
it won't compile (I'd be happy to do that somehow using static_assert).
I would appreciate a hint.
This can be written with the help of two metafunctions.
First, IsContained checks whether a type appears in a type list.
template <typename T, typename... List>
struct IsContained;
template <typename T, typename Head, typename... Tail>
struct IsContained<T, Head, Tail...>
{
enum { value = std::is_same<T, Head>::value || IsContained<T, Tail...>::value };
};
template <typename T>
struct IsContained<T>
{
enum { value = false };
};
Second, IsUnique checks whether a type list contains no duplicates. It uses IsContained to check all element combinations.
template <typename... List>
struct IsUnique;
template <typename Head, typename... Tail>
struct IsUnique<Head, Tail...>
{
enum { value = !IsContained<Head, Tail...>::value && IsUnique<Tail...>::value };
};
template <>
struct IsUnique<>
{
enum { value = true };
};
With these tools, the static assertion is then very simple:
template <typename... Ts>
struct NoDuplicates
{
static_assert(IsUnique<Ts...>::value, "No duplicate types allowed");
};
If you have access to C++1z fold expressions, you can simplify #TheOperator's answer by doing the following for IsContained:
template <typename T, typename... List>
struct IsContained;
template <typename T, typename Head, typename... Tail>
struct IsContained<T, Head, Tail...>
{
enum { value = std::is_same<T, Head>::value || (IsContained<T, Tail>::value && ... && true) };
};
template <typename T>
struct IsContained<T>
{
enum { value = false };
};
The difference is that with the fold expression there is a larger chance of re-use of the class instantiations. If you are using a lot of parameters or have many repeat comparisons, this might have a faster compilation time. As always, test it out yourself.
With c++17 this is pretty easy.
#include <type_traits>
template <typename T, typename... Ts>
struct are_distinct: std::conjunction<
std::negation<std::is_same<T, Ts>>...,
are_distinct<Ts...>
>{};
template <typename T>
struct are_distinct<T>: std::true_type{};
The algorithm is the same as the one illustrated by Jkor and TheOperator, but expressed much more concisely.
Then, coming to your specific question, you'd use it this way:
template<typename ...Types>
struct MyVariadicTemplate {
static_assert(are_distinct<Types...>::value, "Types must be distinct");
/* rest of the code */
};
However, given this is O(n²), I wonder if there's a way to make it less algorithmically complex? Boost::mpl's unique<> is O(n), but I can't get my head around the algo it uses.
If this is a duplicate I apologize. I looked around and found similar issues but nothing exactly like this.
If I instantiate a template like so...
MyClass<int[10]> c;
How can I write the template to get access to both the type and the array size? I've tried everything I can think of and I can't get it.
I was inspired by the std::function template which lets you use similar syntax to the function prototype, like...
std::function<int(MyClass&)> myfunc;
So I thought it would be nice to have something similar for the array and its size. I can use any of the newest c++ features (c++ 11/14).
You can add a partial specialization which looks like this:
template <typename T, ptrdiff_t N>
class MyClass<T[N]>
{
};
Here's a demo.
template<class Arr>
struct array_size {};
template<class T, size_t N>
struct array_size<T[N]>:std::integral_constant<std::size_t, N>{};
template<class Arr>
struct array_element {};
template<class Arr>
using array_element_t = typename array_element<Arr>::type;
template<class T, size_t N>
struct array_element<T[N]>{using type=T;};
now you can array_size<ArrType>{} and array_element_t<ArrType> without unpacking the type.
template <typename T, typename = void>
struct deduce
{
};
template <typename T>
struct deduce<T,
typename ::std::enable_if<
::std::is_array<T>{}
>::type
>
{
using value_type =
typename ::std::decay<decltype(::std::declval<T>()[0])>::type;
static constexpr auto size = sizeof(T) / sizeof(value_type);
};
This is more of a conceptual question. I'm trying to find the easiest way of converting a two-arg template (the arguments being types) into a one-arg template. I.e., binding one of the types.
This would be the meta-programming equivalent of bind in boost/std. My example includes a possible use-case, which is, passing std::is_same as template argument to a template that takes a one-arg template template argument (std::is_same being a two-arg template), i.e. to TypeList::FindIf. The TypeList is not fully implemented here, neither is FindIf, but you get the idea. It takes a "unary predicate" and returns the type for which that predicate is true, or void if not such type.
I have 2 working variants but the first is not a one-liner and the 2nd uses a rather verbose BindFirst contraption, that would not work for non-type template arguments. Is there a simple way to write such a one-liner? I believe the procedure I'm looking for is called currying.
#include <iostream>
template<template<typename, typename> class Function, typename FirstArg>
struct BindFirst
{
template<typename SecondArg>
using Result = Function<FirstArg, SecondArg>;
};
//template<typename Type> using IsInt = BindFirst<_EqualTypes, int>::Result<Type>;
template<typename Type> using IsInt = std::is_same<int, Type>;
struct TypeList
{
template<template<typename> class Predicate>
struct FindIf
{
// this needs to be implemented, return void for now
typedef void Result;
};
};
int main()
{
static_assert(IsInt<int>::value, "");
static_assert(!IsInt<float>::value, "");
// variant #1: using the predefined parameterized type alias as predicate
typedef TypeList::FindIf<IsInt>::Result Result1;
// variant #2: one-liner, using BindFirst and std::is_same directly
typedef TypeList::FindIf< BindFirst<std::is_same, int>::Result>::Result Result2;
// variant #3: one-liner, using currying?
//typedef TypeList::FindIf<std::is_same<int, _>>::Result Result2;
return 0;
}
Click here for code in online compiler GodBolt.
I think the typical way of doing this is keep everything in the world of types. Don't take template templates - they're messy. Let's write a metafunction named ApplyAnInt that will take a "metafunction class" and apply int to it:
template <typename Func>
struct ApplyAnInt {
using type = typename Func::template apply<int>;
};
Where a simple metafunction class might be just checking if the given type is an int:
struct IsInt {
template <typename T>
using apply = std::is_same<T, int>;
};
static_assert(ApplyAnInt<IsInt>::type::value, "");
Now the goal is to support:
static_assert(ApplyAnInt<std::is_same<_, int>>::type::value, "");
We can do that. We're going to call types that contain _ "lambda expressions", and write a metafunction called lambda which will either forward a metafunction class that isn't a lambda expression, or produce a new metafunction if it is:
template <typename T, typename = void>
struct lambda {
using type = T;
};
template <typename T>
struct lambda<T, std::enable_if_t<is_lambda_expr<T>::value>>
{
struct type {
template <typename U>
using apply = typename apply_lambda<T, U>::type;
};
};
template <typename T>
using lambda_t = typename lambda<T>::type;
So we update our original metafunction:
template <typename Func>
struct ApplyAnInt
{
using type = typename lambda_t<Func>::template apply<int>;
};
Now that leaves two things: we need is_lambda_expr and apply_lambda. Those actually aren't so bad at all. For the former, we'll see if it's an instantiation of a class template in which one of the types is _:
template <typename T>
struct is_lambda_expr : std::false_type { };
template <template <typename...> class C, typename... Ts>
struct is_lambda_expr<C<Ts...>> : contains_type<_, Ts...> { };
And for apply_lambda, we just will substitute the _ with the given type:
template <typename T, typename U>
struct apply_lambda;
template <template <typename...> class C, typename... Ts, typename U>
struct apply_lambda<C<Ts...>, U> {
using type = typename C<std::conditional_t<std::is_same<Ts, _>::value, U, Ts>...>::type;
};
And that's all you need actually. I'll leave extending this out to support arg_<N> as an exercise to the reader.
Yeah, I had this issue to. It took a few iterations to figure out a decent way to do this. Basically, to do this, we need to specify a reasonable representation of what we want and need. I borrowed some aspects from std::bind() in that I want to specify the template that I wish to bind and the parameters that I want to bind to it. Then, within that type, there should be a template that will allow you to pass a set of types.
So our interface will look like this:
template <template <typename...> class OP, typename...Ts>
struct tbind;
Now our implementation will have those parameters plus a container of types that will be applied at the end:
template <template <typename...> class OP, typename PARAMS, typename...Ts>
struct tbind_impl;
Our base case will give us a template type, which I'll call ttype, that'll return a template of the contained types:
template <template <typename...> class OP, typename...Ss>
struct tbind_impl<OP, std::tuple<Ss...>>
{
template<typename...Us>
using ttype = OP<Ss...>;
};
Then we have the case of moving the next type into the container and having ttype refer to the ttype in the slightly simpler base case:
template <template <typename...> class OP, typename T, typename...Ts, typename...Ss>
struct tbind_impl<OP, std::tuple<Ss...>, T, Ts...>
{
template<typename...Us>
using ttype = typename tbind_impl<
OP
, std::tuple<Ss..., T>
, Ts...
>::template ttype<Us...>;
};
And finally, we need a remap of the templates that will be passed to ttype:
template <template <typename...> class OP, size_t I, typename...Ts, typename...Ss>
struct tbind_impl<OP, std::tuple<Ss...>, std::integral_constant<size_t, I>, Ts...>
{
template<typename...Us>
using ttype = typename tbind_impl<
OP
, typename std::tuple<
Ss...
, typename std::tuple_element<
I
, typename std::tuple<Us...>
>::type
>
, Ts...
>::template ttype<Us...>;
Now, since programmers are lazy, and don't want to type std::integral_constant<size_t, N> for each parameter to remap, we specify some aliases:
using t0 = std::integral_constant<size_t, 0>;
using t1 = std::integral_constant<size_t, 1>;
using t2 = std::integral_constant<size_t, 2>;
...
Oh, almost forgot the implementation of our interface:
template <template <typename...> class OP, typename...Ts>
struct tbind : detail::tbind_impl<OP, std::tuple<>, Ts...>
{};
Note that tbind_impl was placed in a detail namespace.
And voila, tbind!
Unfortunately, there is a defect prior to c++17. If you pass tbind<parms>::ttype to a template that expects a template with a particular number of parameters, you will get an error as the number of parameters don't match (specific number doesn't match any number). This complicates things slightly requiring an additional level of indirection. :(
template <template <typename...> class OP, size_t N>
struct any_to_specific;
template <template <typename...> class OP>
struct any_to_specific<OP, 1>
{
template <typename T0>
using ttype = OP<T0>;
};
template <template <typename...> class OP>
struct any_to_specific<OP, 2>
{
template <typename T0, typename T1>
using ttype = OP<T0, T1>;
};
...
Using that to wrap tbind will force the compiler to recognize the template having the specified number of parameters.
Example usage:
static_assert(!tbind<std::is_same, float, t0>::ttype<int>::value, "failed");
static_assert( tbind<std::is_same, int , t0>::ttype<int>::value, "failed");
static_assert(!any_to_specific<
tbind<std::is_same, float, t0>::ttype
, 1
>::ttype<int>::value, "failed");
static_assert( any_to_specific<
tbind<std::is_same, int , t0>::ttype
, 1
>::ttype<int>::value, "failed");
All of which succeed.