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の特定のバージョンで私と一緒に、正確なエラーテキスト(使用する関数をオーバーロードしているかを把握することができない程度のもの)を持っていませんUbuntu)、しかし私はそれを得ると、このポストを更新します。

その間、g ++が提供されている関数の型を解決できない理由を誰でも説明できますか?私のマシンで

+0

は(4.3.3-5ubuntu4 ++ g)の問題もなく私のためにコンパイルします。平らな壁でも警告は出ません。 – sth

+0

私のために、Debian、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.