ich mit gcc 4.9.2
in ein Problem laufen (mit -std = C++ 11) nicht ein Stück Code mit der Fehlermeldung Kompilierung wirdGCC 4.9 mehrdeutig Überlastung Template-Spezialisierung
Aufruf überladener ‚InsertDataIntoInputMap (int & boost :: shared_ptr &)‘mehrdeutig ist
Der Code mit msvc 2013 nicht kompilieren
#include <iostream>
#include <map>
#include <boost/shared_ptr.hpp>
struct Proxy
{
typedef std::map<int, int> InputDataMap;
int a;
};
template<class C, class D>
void InsertDataIntoInputMap(
const typename C::InputDataMap::key_type& key,
const D val)
{
std::cout << "Not shared\n";
}
template<class C, class D>
void InsertDataIntoInputMap(
const typename C::InputDataMap::key_type& key,
const boost::shared_ptr<D> val)
{
if (val)
{
std::cout << "Shared\n";
}
}
int main() {
int a;
boost::shared_ptr<double> x(new double(4.5));
InsertDataIntoInputMap<Proxy>(a, x);
}
während die folgende ist tatsächlich sowohl mit gcc und msvc kompiliert:
#include <iostream>
#include <boost/shared_ptr.hpp>
template<class C, class D>
void InsertDataIntoInputMap(
const C& key,
const D val)
{
std::cout << "Not shared\n";
}
template<class C, class D>
void InsertDataIntoInputMap(
const C& key,
const boost::shared_ptr<D> val)
{
if (val)
{
std::cout << "Shared\n";
}
}
int main() {
int a = 0;
boost::shared_ptr<double> x(new double(4.5));
InsertDataIntoInputMap<int>(a, x);
return 0;
}
Ich hätte gedacht, dass der Compiler die Funktion mit dem boost :: shared_ptr Argumente in beiden Fällen nehmen soll?
Coliru kompiliert das erste Beispiel mit clang und g ++ nicht. http://coliru.stacked-crooked.com/a/d1035dd01513af3a – NathanOliver
Ich denke, es hat etwas damit zu tun, dass 'C :: InputDataMap :: key_type' ein nicht-abgeleiteter Kontext ist, aber meine manuelle Ausführung von Template-Deduktion und Bestellung ist nicht gut genug, um dies durchzuarbeiten. – TartanLlama
Ich beginne zu denken, dass Clang und GCC hier falsch sind, da die zweite Template-Funktion mindestens so spezialisiert für einen Parameter und spezialisierter für den anderen ist, also unabhängig von nicht-abgeleiteten Kontexten ausgewählt werden sollte. Kann jemand eine bessere Erklärung liefern? – TartanLlama