If constexpr instead of sending a tag

I want to use if constexpr instead of tag dispatching, but I'm not sure how to use it. Sample 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 this bool supported = MyTag<T>::Supported; // I want to do something like this if constexpr (T == std::uint64_t) supported = true; } }; 
+5
source share
1 answer

One way is to define the predicate constexpr, which checks the type of its argument, and then constexpr to include the result of this predicate.

I think this method is good because it separates functional logic from precondition logic.

 #include <iostream> #include <cstddef> #include <type_traits> class MyTest { public: template<typename T> void do_something(T value) { // define our predicate // lambdas are 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 the result of the 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)); // be careful with std::uint_xx aliases } 

Examples of results:

 not supported supported not supported supported 

Another way to express this could 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"; } } }; 
+4
source

Source: https://habr.com/ru/post/1271714/


All Articles