게시 한 예제 코드가 컴파일되지 않고 (typename std::string::const_iterator
은 간단히 std::string::const_iterator
이어야 함)이 오류를 수정하면 코드가 run as expected으로 바뀌 었으므로 실제 코드가 조금 다를 것입니다.
std::string
매개 변수가 값으로 복사되고 const_iterator
에 액세스하기 전에 참조 계산이 어떻게 든 중단되는 경우처럼 들립니다. 예를 들어,이 서명 :
std::string::const_iterator return_iterator(std::string line)
//^Notice: Not passing by
// reference
값으로 문자열을 전달합니다. COW 때문에 복사본이 공유되지만 함수가 line.begin()
을 호출하는 순간 비 const 문자열 멤버 함수 std::string::begin()
이 호출됩니다. 이는 기본 문자열의 새 복사본이 일반적으로 만들어지는 것을 의미합니다. 비 const iterator
이 반환되면 암시 적으로 const_iterator
(완벽하게 유효한 변환)으로 변환됩니다.
편집 :
#include <cassert>
#include <string>
#include <iostream>
std::string::const_iterator return_iterator(std::string line)
{
std::string::const_iterator beg = line.begin();
std::cout << "In return_iterator(), &(*line.begin()) is " << static_cast<const void *>(&*beg) << '\n';
/*here I'm moving this iterator and assing to it i.e.*/
beg = line.begin() + 3;
return beg;//at this point it points to a character
}
void other_fnc(const std::string& line)
{
std::string::const_iterator pos = return_iterator(line);//after this fnc returns pos points to some rubbish
std::cout << "In other_fnc(), &(*line.begin()) is " << static_cast<const void *>(&*line.begin()) << '\n';
assert(*pos == line[3]);
}
int main()
{
std::string line = "This is a test.";
other_fnc(line);
}
http://codepad.org/K9yaWqWA
을 주장 :는 값으로 return_iterator()
에 line
매개 변수를 전달하기 위해 수정 된 다음 버전의 출력을 살펴, 내 점을 보여주기 위해 이제 실패합니다. 또한 *line.begin()
의 주소가 다릅니다.
컴파일하고 문제를 보여줍니다 최소한의 케이스를 제공하세요. –
"일부 위치"가 문자열의 길이를 초과하지 않는 경우 (즉, 같거나 지나치게 :: end()) 이상이 아닌 경우가 될 수 있습니다. –