c++ - Is there a nicer workaround for enable_if bug in CLang (Bug 11723)? -


ideally, can enable_if:

#include <type_traits>  namespace detail {     enum class enabler_t { dummy }; }  template<bool b> using enable_if_u = typename std::enable_if<b, detail::enabler_t>::type;  template<bool b> using disable_if_u = typename std::enable_if<!b, detail::enabler_t>::type;  template<typename t, enable_if_u<std::is_same<t, int>::value>...> int a(){return 0;}  template<typename t, disable_if_u<std::is_same<t, int>::value>...> double a(){return 0.0;}  int main() {     auto x = a<int>(); } 

which is, imho, nicest way use it.

however, doesn't work clang, due bug 11723.

the workaround i'm using using dummy constexpr variable:

namespace detail {     enum class enabler_t { dummy };     constexpr const enabler_t dummy = enabler_t::dummy; }  //...  template<typename t, enable_if_u<std::is_same<t, int>::value> = detail::dummy> int a(){return 0;}  template<typename t, disable_if_u<std::is_same<t, int>::value> = detail::dummy> double a(){return 0.0;}  //... 

i'm not huge fan of workaround wondering if there workaround make code working in clang ? i'm looking pure c++ solution, no preprocessor involved.

the example in here illustrate problem, not useful @ , there tons of way make better. want find nicer workaround enable_if part.

you should use macro instead:

#define enable_if(...) typename std::enable_if<(__va_args__), detail::enabler_t>::type=detail::enabler_t::dummy 

the macro has advantage of avoiding vexing parse in c++, since can place parenthesis around boolean expression. if think uppercase macro looks ugly, can use zlang make cleaner, this:

#define bw_enable_if(...) typename std::enable_if<(__va_args__), detail::enabler_t>::type=detail::enabler_t::dummy #define zlang_bw_enable_if (bw_enable_if) 

then can write following if compile -dzlang_ns=bw:

template<typename t, $(enable_if std::is_same<t, int>())> int a(){return 0;} 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -