Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can you achieve the same thing without template specialization?

Let’s say I need to make a type that accepts a new types the same way as the containers work.

Can I achieve the same thing without push_back type uses template specialization?

#include <iostream>
template <typename...>
struct type_list {};

template <typename T>
struct my_type { using type = T; };


template <typename LIST, typename T>
struct push_back;

template <typename... Args, typename T>
struct push_back<type_list<Args...>, T> : my_type<type_list<Args..., T>> 
{
};

template <typename LIST, typename T>
using push_back_t = typename push_back<LIST, T>::type;


int main()
{
    type_list<int, bool> x;
    push_back_t<decltype(x), float> y;
    type_list<int, bool,float> z;
    std::cout << std::is_same_v<decltype(y), decltype(z)> << '\n';
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can make it a function template to avoid specialization and make it a bit nicer to use:

template<typename T, typename... Args>
auto push_back(type_list<Args...>) {
    return type_list<Args..., T>{};
}

...

auto y = push_back<float>(x);

This is the basis of what Boost.Hana brought to the table for metaprogramming—using empty values whose types contain the desired information. It’s not always this pretty, but it’s surprisingly powerful. Hana is the proof that you can make algorithms fit this format as well, allowing operations like filter to be implemented with similar levels of ease once you have the groundwork for them.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading