2012-02-26 2 views
2

다음 두 경우의 차이점은 무엇입니까?boost :: bind with maps, 바인딩 std :: pair와 std :: map :: value_type의 차이점은 무엇입니까?

std::pair<int,std::string> example_1 (std::make_pair (1,"foo")); 
int value_1 = boost::bind (&std::pair<int,std::string>::first,_1) (example_1); 

std::map<int,std::string>::value_type example_2 (std::make_pair (2,"boo")); 
int value_2 = boost::bind (&std::pair<int,std::string>::first,_1) (example_2); 

첫 번째 예제는 제대로 작동하지만 바인딩이 완료되면 두 번째 예제는 컴파일되지 않습니다.

typedef std::pair<const _Key, _Tp> value_type; 

나는 그 차이를 볼 수 없습니다 다음과 같이 나는 파일 stl_map.h에보고하고 value_type가 정의됩니다. 나는 두 번째 예제가 컴파일되지 않는 이유와 그 이유를 누군가에게 알려주는 것에 감사 할 것입니다.

컴파일 에러 메시지는 다음과 같습니다 사전에

.../include/boost/bind/mem_fn.hpp:333:36: error: no matching function for call to ‘get_pointer(const std::pair<const int, std::basic_string<char> >&)’ 
make: *** [main.o] Error 1 

감사합니다! 당신이 사용하고있는 쌍 (귀하의 경우 일반 int)를하지 않는 반면

답변

1

mapvalue_typeconst 키 (귀하의 경우 const int)가 있습니다.

+0

맞아요! 나는 'const'를 깨닫지 못했다. Gracias! – user1192525

2

차이점은지도 값 유형에서 const을 사용하는 것입니다. std::pair<int,std::string>::firststd::pair<const int,std::string>::first의 접근 가능한 아이템이 아닙니다. 예, const 버전에서 non-const 버전으로 쌍으로 정의 된 암시 적 변환이 있지만이 변환은 이와 같은 멤버 함수를 호출하기 위해 고려되지 않습니다. pair의 사용법은 비슷할 수 있지만 실제로는 필드 위치 및 기타 위치와 관련이없는 완전히 독립적 인 클래스입니다. 본질적으로

는 you'e가 유효하지 않습니다

std::pair<const int,std::string> example(3, "Hello"); 
example.std::pair<int,std::string>::first 

과 같은 코드를 구축하기 위해 후원을 요청했다.

+0

그래, 나는'const'을 깨닫지 못했다 ... 커피를 더 적다. 감사! – user1192525

관련 문제