c++ - if constexpr instead of tag dispatch -
i want use if constexpr
instead of tag dispatching, not sure how use it. example code below.
template<typename t> struct mytag { static const int supported = 0; }; template<> struct mytag<std::uint64_t> { static const int supported = 1; }; template<> struct mytag<std::uint32_t> { static const int supported = 1; }; class mytest { public: template<typename t> void do_something(t value) { // instead of doing bool supported = mytag<t>::supported; // want if constexpr (t == std::uint64_t) supported = true; } };
one way define constexpr predicate checks type of argument, constexpr switch on result of predicate.
i think way nice because separates functional logic precondition logic.
#include <iostream> #include <cstddef> #include <type_traits> class mytest { public: template<typename t> void do_something(t value) { // define our predicate // lambdas constexpr-if-possible in c++17 constexpr auto is_supported = [](auto&& x) { if constexpr (std::is_same<std::decay_t<decltype(x)>, std::uint64_t>()) return true; else return false; }; // use result of predicate if constexpr (is_supported(value)) { std::cout << "supported\n"; } else { std::cout << "not supported\n"; } } }; int main() { auto t = mytest(); t.do_something(int(0)); t.do_something(std::uint64_t(0)); t.do_something(double(0)); t.do_something(static_cast<unsigned long>(0)); // careful std::uint_xx aliases }
example results:
not supported supported not supported supported
another way express might be:
class mytest { public: template<class t> static constexpr bool something_possible(t&&) { return std::is_same<std::decay_t<t>, std::uint64_t>(); } template<typename t> void do_something(t value) { // switch behaviour on result of constexpr predicate if constexpr (something_possible(value)) { std::cout << "supported\n"; } else { std::cout << "not supported\n"; } } };
Comments
Post a Comment