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

Avoid template mess when importing base class constructors of heavily templated base class

This is just short inquiery if it is at all possible to somehow import base class constructors without all the template bloat. Consider this example where I’m inheriting from a templated std::variant:

template <typename StringType, typename Allocator>
class JSON : public std::variant<std::monostate,
                        std::unordered_map<StringType, JSON<StringType, Allocator>, std::hash<JSON<StringType, Allocator>>, std::equal_to<StringType>, Allocator>,
                        std::vector<JSON<StringType, Allocator>, Allocator>, 
                        bool,
                        double,
                        StringType>
{
public:

    using std::variant<std::monostate,
                        std::unordered_map<StringType, JSON<StringType, Allocator>, std::hash<JSON<StringType, Allocator>>, std::equal_to<StringType>, Allocator>,
                        std::vector<JSON<StringType, Allocator>, Allocator>, 
                        bool,
                        double,
                        StringType>::variant;
};

You can see that there’s quite a bit of bloat which makes it rather unreadable and error prone. Can this be avoided? The reason I’m asking is because I think I once heard that you can somehow skip template parameters within a templated class as the compiler can imply what you meant.

Clarification:

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

Essentially I’m just looking for a way to not writing the same template mess twice. I have tried using variant::variant which doesn’t work. But there might be other ways around this and any hints are greatly appreciated!

>Solution :

You could write an alias template to factor out some of the repetition:

template <template <class...> class Temp, class StringType, class Allocator>
using variant_for = std::variant<std::monostate,
                        std::unordered_map<StringType, Temp<StringType, Allocator>, std::hash<Temp<StringType, Allocator>>, std::equal_to<StringType>, Allocator>,
                        std::vector<Temp<StringType, Allocator>, Allocator>, 
                        bool,
                        double,
                        StringType>;

template <typename StringType, typename Allocator>
class JSON : public variant_for<JSON, StringType, Allocator>
{
public:
    using variant_for<JSON, StringType, Allocator>::variant;
};
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