I created function to detect the constness and l(r)valueness of the argument.
template<class T> std::string
detect(typename std::remove_reference<T>::type&&) {
return std::string(std::is_const<T>::value ? "const " : "") + "rvalue";
}
template<class T> std::string
detect(typename std::remove_reference<T>::type&) {
return std::string(std::is_const<T>::value ? "const " : "") + "lvalue";
}
for some reason, is_const always returns false even on const types, for example const int&. I tried adding another overload to capture the constness
template<class T> std::string
detect(const typename std::remove_reference<T>::type& ) { return "const lvalue"; }
compiler then complains that detect is ambiguous when applied to const int&. So I think the compiler has correct figure out T=const int&, but why doesn’t is_const return true?
std::is_const<T>only detects top-levelconst. Likefoo const, orfoo* const. It doesn’t care about “inner”consts, likefoo const*orfoo const&.If what you want is to see if type a reference to const, you need to take out the reference first, so the
constbecomes top-level:In any case, the functions shown do not allow type deduction, meaning you have to pass
Texplicitly, likedetect<foo const&>(x). Maybe you want something like the following?Which can be called like
detect(x).