2009-06-15 6 views
0

지도에서 매핑 된 값을 사용하여 요소 벡터를 작성하려는 코드가 있습니다. 아래 코드는 Visual Studio에서 잘 작동하지만 (가능한 한 합법적 인 것처럼 보이지만) g ++에는 동의하지 않습니다.g ++의 템플릿 펑터 오류

template<class PAIR> 
typename PAIR::second_type foo(const PAIR& arg) 
{ 
    return (arg.second); 
} 

class A 
{ 
private: 
    typedef std::map<int, std::wstring> map_t; 
    map_t m_map; 

public: 
    void bar() 
    { 
     // Attempt to pulled the mapped type from the map into the vector 
     std::vector<std::wstring>vect(m_map.size()); 
     std::transform(m_map.begin(), m_map.end(), vect.begin(), 
      &foo<map_t::value_type>); // <-- error here, see below, also 

     // other attempts that all failed: 
     // - std::transform(..., boost::bind(foo<map_t::value_type>, _1)); 
     // - std::transform(..., boost::bind(&map_t::value_type::second, _1)); 
     // - also tried casting foo to a specific function type 
     // - also tried "template<class T> T itself(T arg) { return T; }" applied to all the above functor candidates, a la "std::transform(..., itself(<<functor>>));" 
    } 
}; 

불행하게도, 나는 나와 함께 순간 또는 g의 특정 버전에서 (사용하는 기능을 오버로드 알아낼 수 없다는 대해 뭔가를) 정확한 오류 텍스트가없는 ++ (최신 존재와 함께 배포 우분투),하지만 내가 그것을 얻을 때이 게시물을 업데이 트됩니다.

그동안 g ++이 제공되는 펑터의 유형을 해결할 수없는 이유를 설명 할 수 있습니까? 내 컴퓨터에

+0

이 (4.3.3-5ubuntu4 ++ g) 문제없이 나를 위해 컴파일합니다. 심지어 - 벽은 경고를주지 않습니다. – sth

+0

나를 위해, 데비안, gcc 4.1, 4.2 및 4.3 – jpalecek

+0

당신이 사용하는 gcc의 정확한 포함 파일과 버전이 있습니까? –

답변

1

다음 컴파일 :

#include <map> 
#include <vector> 
#include <algorithm> 
#include <string> 

template<class PAIR> 
typename PAIR::second_type foo(const PAIR& arg) 
{ 
    return (arg.second); 
} 

class A 
{ 
private: 
    typedef std::map<int, std::wstring> map_t; 
    map_t m_map; 

public: 
    void bar() 
    { 
     std::vector<std::wstring>vect(m_map.size()); 
     std::transform(m_map.begin(), m_map.end(), vect.begin(), 
      &foo<map_t::value_type>); 
    } 
}; 

명령 행 :

g++ -c overt.cpp 

버전 :

$ g++ --version 
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490) 
Copyright (C) 2005 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.