2016-11-15 4 views
4

범용 참조 (T&&)는 어떤 종류의 참조도 받아 들여야한다고 생각했습니다. 그러나 다음은 작동하지 않습니다.lvalue를 A에 바인딩 할 수 없습니다 <Cv2> &&

필자가 쓰고있는 라이브러리에서 const-correct를 시도하면이 문제가 발생합니다. 나는 C++을 처음 접했고 전에 이런 식으로 보지 못했습니다.

Test.cpp에 : (g++ test.cpp -std=c++11 컴파일)

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); 
} 

오류 :

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) {} 
     ^

그런데, 실제 프로그램에서 class A는 실제 데이터를 소유하고, 포함되지 않은 참조 실제로 데이터를 보유하고있는 다른 클래스로 임시 객체를 허용하기 위해 tclass A의 멤버 함수가 허용 될 때 이것이 간접 지정/데이터 복사를 지속적으로 생성하지 않는다는 의미입니다.

+3

. &&는 범용 참조가 아니며 rvalue 참조입니다. T &&는 T가 호출 된 유형에 의해 추론 된 템플릿 매개 변수 인 경우입니다. 즉, T는 템플릿 매개 변수 여야하며 함수 AND에 대한 매개 변수는 지정되지 않고 추론되어야합니다. –

+0

그래서'std :: vector &&'또는'class_name