Home Function template specialization with templated typename
Reply: 2

Function template specialization with templated typename

Kosterio
1#
Kosterio Published in 2017-12-06 09:05:27Z

I have a template method in some class

template<typename T> int get(T &value) const {
    ...
}

And several specializations

template<> int AAA::get<int>(int &val) const;
template<> int AAA::get<AAA>(AAA &val) const;

There is a templated type

template<const int SIZE> class BBB{
    ...
};

and I need to specialize my template method with this type.

template<> int AAA::get<BBB<SIZE>>(BBB<SIZE> &val) const;

I know that function template partial specialization is disabled. But maybe there be a solution for this particular case?

VTT
2#
VTT Reply to 2017-12-06 09:13:36Z

You can turn it into template class specialization:

class AAA
{
    template<typename T> class get_impl
    {
        public: static int get(T & value) { return(0); }
    };

    public: template<typename T> int get(T & value) const
    {
        return(get_impl<T>::get(value));
    }
};

template<> class AAA::
get_impl<int>
{
    public: static int get(int & value) { return(0); }
};

template<> class AAA::
get_impl<AAA>
{
    public: static int get(AAA & value) { return(0); }
};

template<int SIZE> class BBB{};

template<int SIZE> class AAA::
get_impl<BBB<SIZE>>
{
    public: static int get(BBB<SIZE> & value) { return(0); }
};

int main()
{
    AAA a{};
    BBB<5> b{};
    a.get(b);
}
Jarod42
3#
Jarod42 Reply to 2017-12-06 09:14:28Z

Instead of specialization, use overloads:

int AAA::get(int &val) const;
int AAA::get(AAA &val) const;
template <int Size> int AAA::get(BBB<SIZE> &val) const;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318156 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO