Unable to bind lvalue to A <Cv2> &&
I thought that universal link ( T&&) should accept any links. But the following does not work.
I encounter this problem when I try to be const-correct in the library that I am writing. I am new to C ++ and have never seen anything like it before.
test.cpp:
enum Cv_qualifier {
constant,
non_const
};
template <Cv_qualifier Cv> class A;
template<>
class A<Cv_qualifier::constant> {
public:
template<Cv_qualifier Cv2>
void t(const A<Cv2>&& out) {}
};
template <>
class A<Cv_qualifier::non_const> {
public:
template<Cv_qualifier Cv2>
void t(const A<Cv2>&& out) {}
};
int main()
{
A<Cv_qualifier::non_const> a;
A<Cv_qualifier::constant> b;
a.t(b);
}
Error (compiled with g++ test.cpp -std=c++11):
test.cpp: In function ‘int main()’:
test.cpp:24:10: error: cannot bind ‘A<(Cv_qualifier)0u>’ lvalue to ‘const A<(Cv_qualifier)0u>&&’
a.t(b);
^
test.cpp:17:10: note: initializing argument 1 of ‘void A<(Cv_qualifier)1u>::t(const A<Cv2>&&) [with Cv_qualifier Cv2 = (Cv_qualifier)0u]’
void t(const A<Cv2>&& out) {}
^
By the way, in a real program it class Adoes not contain any real data and contains links to another class that actually stores data. Hopefully this means that I do not constantly create indirect / copies of data when I allow the member function tof class Ato accept temporary objects.
- . :
T&& & -> T&
T& && -> T&
T&& && -> T&&
, T&& , rvalue T. , , SomeType&& SomeType&& rvalue.
, , , :
template <Cv_qualifier Cv> struct A;
template<>
struct A<Cv_qualifier::constant> {
template<typename T>
void t(T&& out) {}
};
template <>
struct A<Cv_qualifier::non_const> {
template<typename T>
void t(T&& out) {}
};
, . Cv_qualifier T, , :
template<typename>
struct CvValue;
template<Cv_qualifier cv>
struct CvValue<A<cv>> {
constexpr static Cv_qualifier value = cv;
};
T :
// v----- This is a good practice to apply a constraint
template<typename T, std::void_t<decltype(CvValue<std::decay_t<T>>::value)>* = 0>
auto t(T&& out) {
constexpr auto cv = CvValue<std::decay_t<T>>::value;
// do whatever you want with cv
}
++ 17 std::void_t, :
template<typename...>
using void_t = void;
, , T A<...>, :
template<typename>
struct is_A : std::false_type {};
template<Cv_qualifier cv>
struct is_A<A<cv>> : std::true_type {};
std::decay_t:
template<typename T, std::enable_if_t<std::is_A<std::decay_t<T>>::value>* = 0>
void t(T&& out) {}