std::vector<std::shared_ptr<const T>>
에 복사하고 싶은
std::vector<std::shared_ptr<T>>
지금 나는 것으로 나타났습니다 경우 I 이 작업을 수행 : 그것은 잘 컴파일
class A
{
public:
A(const std::vector<std::shared_ptr<int>>& list) : internalList(list.begin(), list.end()) {}
std::vector<std::shared_ptr<const int>> internalList;
};
(연타 ++ 표준을 == C++ 14)하지만 내가 할 경우 :
class A
{
public:
A(const std::vector<std::shared_ptr<int>>& list) : internalList(list) {}
std::vector<std::shared_ptr<const int>> internalList;
};
복사 생성자를 사용할 때 non-const에서 const 로의 변환을 알아낼 수 없기 때문에 복사 생성자를 사용할 때 이상하다는 것을 알았습니까?
xxxx.cpp:672:56: error: no matching constructor for initialization of 'std::vector<std::shared_ptr<const int> >'
사람은 왜하시기 바랍니다 설명 할 수 있고, 나는 그것이 (생성자에서 반복자를 사용) 할 방법이 최선의 해결책 인 경우?
std::vector
가 변환 생성자를 제공하는 경우 코드가 작동 할 수
비 const에서 const 로의 변환 (즉, int int에서 const int 로의 변환)이면 괜찮습니다. 그러나 그것은 여기서 일어나는 것이 아닙니다. 한 유형의 객체에서 완전히 다른 유형의 객체로의 변환입니다. 복사 생성자는 다른 유형이 아닌 인수로만 자체 유형을 취합니다. –
좋아요,하지만이 작품과 같은 것이 아닙니다. 'int a = 1; const int b = a'? 그래서 내 질문에 왜이 할당 작동하고 왜 복사 생성자를 사용하여 수행 할 수 없습니다? 이 일을 막는 것이 무엇인지 정말 궁금합니다. 최적화를 위해 완료되지 않았다고 가정합니다 ... – user18490
@ user18490'const std :: vector'은'std :: vector '과 같지 않습니다. 그러므로'int'와'const int'는 유추가되지 않습니다. –