c++ - Type based on template argument value -


i have template function this:

enum class myenum { enum_1, enum_2, enum_3 };  template<myenum e, typename t> void func( int ) {     std::vector<t> somedata = ......;     t somevalue;      switch( e )     {         case enum_1:             somevalue += func1( somedata );             break;         case enum_2:             somevalue += func2( somedata );             break;         case enum_3:             somevalue += func3( somedata );             break;     } } 

the type t dependent on value of e. i'd write code like

template<myenum e> void func( int ) {     if( e = myenum:enum_1 ) t = char;     else t = float;      std::vector<t> somedata = ......;     t somevalue;      switch( e )     {         case enum_1:             somevalue += func1( somedata, ..... );             break;         case enum_2:             somevalue += func2( somedata, ..... );             break;         case enum_3:             somevalue += func3( somedata, ..... );             break;     } } 

i can see how make type dependent on type, e.g.

typedef std::conditional<std::is_same<t1, float>::value, char, float>::type t; 

but can't figure out how extend cope enum values. possible want to? if so, how?

note: func1, func2 , func3 fixed , beyond control.

thanks!

the first template parameter std::conditional plain old bool, can shove logic in there:

using t = typename std::conditional<(e == myenum::enum_1), char, float>::type; using t = std::conditional_t<(e == myenum::enum_1), char, float>; //c++14 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -