简体   繁体   中英

template class specialization - how to pass the template argument to constructor?

I have the following code (simplified here for readability):

enum id_t {B, C, D};

template <id_t id, int16_t value> class class_name;

template <int16_t value>
class class_name<B, value>
{ 
public:
    void member_func() { /* do something related to B */ }
};

template <int16_t value>
class class_name<C, value>
{ 
public:
    void member_func() { /* do something related to C */ }
};

template <int16_t value>
class class_name<D, value>
{ 
public:
    void member_func() { /* do something related to D */ }
};

The code above works fine. I do something like this and it's ok:

class_name<B, 5> obj_b;
class_name<C, 3> obj_c;
class_name<D, 1> obj_d;

obj_b.member_func();
obj_c.member_func();
obj_d.member_func();

I also need to instantiate class_name as argument to functions, and it works, despite the ugly syntax:

do_something_with_class_name_objs(class_name<D, 0>(), class_name<C, 2>());

Because I want the things as easy as possible, to other people to use, I was trying to make the template arguments implicit, just like we do when we call function templates with arguments and the compiler knows how it need to instantiate that function just looking at the function arguments, not the template ones.

I want to instantiate my class_name this way:

class_name obj_b(B, 5);
class_name obj_c(C, 3);
class_name obj_d(D, 1);

So I could be able to instantiate it as argument to functions this way:

do_something_with_class_name_objs(class_name(D, 0), class_name(C, 2));

It is much more readable, IMHO.

I have tried to do change my template specializations in some ways. None of the constructors in the following template worked:

template <int16_t value>
class class_name<B, value>
{ 
public:
    class_name(id_t id, int16_t value) {}           // don't work
    class_name(B, value) {}                         // don't work
    class_name<B, value>(id_t id, int16_t value) {} // don't work  
    void member_func() { /* do something related to B */ }
}; 

What is the right way to do that?

The template arguments of a class template cannot be deduced from a constructor call... until the upcoming C++17 standard. See Class template deduction .

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