Is There Anything Like a Templatized Case-Statement

后端 未结 5 974
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 18:18

So I have this really ugly code:

template 
std::conditional_t

        
5条回答
  •  无人及你
    2020-12-16 18:18

    The template version of a switch statement is a specialized template.

    template struct matching_type;
    template<> struct matching_type { typedef char type; };
    template<> struct matching_type { typedef short type; };
    template<> struct matching_type { typedef int type; };
    template<> struct matching_type { typedef long type; };
    template<> struct matching_type { typedef long long type; };
    
    template
    matching_type::type foo(T bar)
    {
        return reinterpret_cast(bar);
    }
    

提交回复
热议问题