c++ - How to convert three functions to one generic function? -
c++ - How to convert three functions to one generic function? -
i have 3 getcolors functions in below sample.
question : how convert threee getcolors functions 1 generic function?
struct ecvcolormap{ vector<scalar> getcolors(){ vector<scalar> result; //.... homecoming result; } }; struct scalar{ int val[3]; }; vector<scalar> getcolors(vector<scalar> colors){ homecoming colors; } vector<scalar> getcolors(scalar color){ return{ color }; } vector<scalar> getcolors(ecvcolormap color_map){ homecoming color_map.getcolors(); }
the problem is: bodies of functions different , if tried create generic 1 , have others partially specialized , specialized like
template<typename t> vector<scalar> getcolors(t colors) { homecoming {colors}; } //template<typename t> vector<scalar> getcolors(vector<t> colors) { // not necessary - see list initialization // homecoming colors; //} template<> vector<scalar> getcolors<ecvcolormap>(ecvcolormap colors) { homecoming colors.getcolors(); }
you still have write code 3 different templates no gain @ (where's compile-time advantage or code reuse?).
my personal advice: don't utilize templates because it's cool, utilize them when needed. in huge-software view, crucial. design posted makes sense me.
c++ templates generics
Comments
Post a Comment