erreur de modèle dans foncteur g ++
-
13-09-2019 - |
Question
J'ai un code où je voudrais construire un vecteur d'éléments en utilisant les valeurs mappées sur une carte. Le code ci-dessous fonctionne bien dans Visual Studio (et semble légitime pour autant que je peux dire), mais g ++ est en désaccord.
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>>));"
}
};
Malheureusement, je n'ai pas le texte de l'erreur avec moi (quelque chose de ne pas être en mesure de comprendre ce qui surcharge la fonction à utiliser) au moment ou la version spécifique de g ++ (le dernier étant distribué avec Ubuntu), mais Je vais mettre à jour ce post quand je reçois cela.
En attendant, quelqu'un peut-il expliquer pourquoi g ++ ne peut pas résoudre le type de foncteur étant prévu?
La solution
Les compiles suivants sur ma machine:
#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>);
}
};
ligne de commande:
g++ -c overt.cpp
Version:
$ 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.