简体   繁体   中英

How to specialize a template member in a normal class for a template type

I have a class which is not a template but has a template function as follows:

class Table {
public:
 template <typename t>
 t Get(int, int);
};

And I want to specialize this for a template type say for a fixed_string defined like this:

template <int max>
class fixed_string {
};

How can I do that?

As @ForEveR pointed, there's no patial function template specialization, you can only provide a full specialization version for it. Such as:

template <>
fixed_string<9> Table::Get<fixed_string<9>>(int, int);

and then call it by

table.Get<fixed_string<9>>(0, 0);

But you can overload function template, such as:

class Table {
public:
    template <typename t>
    t Get(int, int);
    template <int max>
    fixed_string<max> Get(int, int);
};

and then

Table table;
table.Get<9>(0, 0);

or

class Table {
public:
    template <typename t>
    t Get(int, int);
    template <int max, template<int> class T=fixed_string>
    T<max> Get(int, int);
};

and then

Table table;
table.Get<9>(0, 0); // or table.Get<9, fixed_string>(0, 0);

LIVE

If I understand your question correctly, like this:

Table table;
table.Get<fixed_string<100>>(3,4);

There is no partial function template specialization, so, you can only specialize it for fixed_string with known max.

template<>
fixed_string<100> Table::get(int, int)
{
}

Definition:

template<int max>
 fixed_string<max> Get(int, int);

Call:

Table obj;
fixed_string<20> str = obj.Get<20>(1, 2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM