0

I have a type:

struct A {}

template<typename T>
constexpr bool is_A_v = false;

template<>
constexpr bool is_A_v<A> = true; // here I don't know how to apply std::remove_cvref_t<?> before checking?

I am new to type traits so any help is appreciated.

4
  • If you're using C++20, why not use a concept? Jan 30, 2021 at 14:21
  • 1
    Can you explain what it is you're trying to do? Is the goal of is_A_v<T> to check that std::remove_cvref_t<T> is A?
    – Barry
    Jan 30, 2021 at 15:18
  • Yea, I want to check if something is a type by first cleaning the type, so that references and other things are treated all as A. A, A&, A&&, const A& should all return true.
    – lucmobz
    Jan 30, 2021 at 16:05
  • Do you need to use specialization to implement this? (It does have the advantage of being extensible after the fact.) Jan 30, 2021 at 17:38

1 Answer 1

3

Based on the comment, you're look for:

template <typename T>
inline constexpr bool is_A = std::is_same_v<std::remove_cvref_t<T>, A>;

In C++20, you could spell this as a concept:

template <typename T>
concept is_A = std::same_as<std::remove_cvref_t<T>, A>;

Which has some slight syntactic advantages, but also prevents people from specializing the trait after the fact.

2
  • Why is the inline there? Is it necessary?
    – lucmobz
    Jan 31, 2021 at 8:20
  • 1
    Yes. constexpr variables are not inline implicitly, only constexpr static member variables are inline implicitly.
    – 康桓瑋
    Jan 31, 2021 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.