Một cách là xác định vị từ constexpr kiểm tra loại đối số của nó, sau đó chuyển đổi constexpr thành kết quả của vị từ đó.
Tôi nghĩ rằng cách này là tốt đẹp bởi vì nó tách logic chức năng từ logic điều kiện tiên quyết.
#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
}
kết quả ví dụ:
not supported
supported
not supported
supported
Một cách khác để thể hiện này có thể là:
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";
}
}
};
Nguồn
2017-09-12 07:22:21
Bạn không thể "gọi" 'toán tử == 'trên các loại; điều đó là vô nghĩa. Bạn có thể làm một cái gì đó tương tự với một thư viện như tăng hana: 'nếu constexpr (hana :: type_c == hana :: type_c )'. Bạn cũng có thể sử dụng 'std :: is_same':' nếu constexpr (std :: is_same_v ) ' –
Justin
Các loại không phải là giá trị, bạn không thể so sánh chúng như thế. – Rakete1111
@ Rakete1111 Vâng tôi biết rằng tôi không thể so sánh chúng như thế, đó là câu hỏi là gì, làm thế nào tôi nên làm điều đó .... và tại sao downvote? – 0xBADF00