Kosterio
Kosterio

Reputation: 131

Function template specialization with templated typename

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?

Upvotes: 2

Views: 83

Answers (2)

Jarod42
Jarod42

Reputation: 217115

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;

Upvotes: 2

user7860670
user7860670

Reputation: 37488

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);
}

Upvotes: 2

Related Questions