Template type check parameter C++, different type execution paths -


i'd different logic in template based on template parameter. how might type check template parameter?

i have following surprised not work:

class bar {   bar() {} }  template <typename t> void foo(const &t a) {   if (!std::is_same<t, bar>::value) {     // things fail type bar e.g.     a.fail();   } } 

i do not wish use template specialization in reality template specialization ends sharing lot of code specific purpose (currently working code using template specialization)

currently fails during compile: "no member "fail" in "bar"

each branch should valid every type.
in c++17, use if constexpr change that:

template <typename t> void foo(const &t a) {   if constexpr (!std::is_same<t, bar>::value) {     // things fail type bar e.g.     a.fail();   } } 

before, have rely on specialization or overload. example

template <typename t> void fail(const t& t) { t.fail(); }  void fail(const bar&) { /*empty*/ }  template <typename t> void foo(const &t a) {     // ...     fail(a);     // ... } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -