У меня есть пример кода здесь: http://codepad.org/9JhV7Fuu
С этим кодом:
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
namespace Json {
template <typename ValueType>
struct Value {
Value() {}
Value(int) {}
Value(float) {}
Value(unsigned int) {}
Value(std::string) {}
};
}
template <typename T>
Json::Value<T> toJson(T x) {
return Json::Value<T>(x);
}
template <typename T, typename U>
Json::Value< std::pair<T, U> > toJson(std::pair<T, U> const& rh) {
Json::Value< std::pair<T, U> > return_value;
toJson(rh.first);
toJson(rh.second);
return return_value;
}
template <typename T>
Json::Value<T> toJsonContainer(T const& rh) {
Json::Value<T> return_value;
for (typename T::const_iterator it = rh.begin(); it != rh.end(); ++it) {
toJson(*it);
}
return return_value;
}
template <typename T, typename U>
Json::Value< std::map<T, U> > toJson(std::map<T, U> const& rh) {
return toJsonContainer(rh);
}
template <typename T>
Json::Value< std::vector<T> > toJson(std::vector<T> const& rh) {
return toJsonContainer(rh);
}
int main(int argc, char **argv) {
std::map<std::string, std::vector<unsigned int>> x;
toJson(x);
return 0;
}
Я получаю эту ошибку:
main.cpp: In function ‘Json::Value<T> toJson(T) [with T = std::vector<unsigned int>]’:
main.cpp:27:2: instantiated from ‘Json::Value<std::pair<_T1, _T2> > toJson(const std::pair<_T1, _T2>&) [with T = const std::basic_string<char>, U = std::vector<unsigned int>]’
main.cpp:36:3: instantiated from ‘Json::Value<T> toJsonContainer(const T&) [with T = std::map<std::basic_string<char>, std::vector<unsigned int> >]’
main.cpp:44:27: instantiated from ‘Json::Value<std::map<T, U> > toJson(const std::map<T, U>&) [with T = std::basic_string<char>, U = std::vector<unsigned int>]’
main.cpp:54:10: instantiated from here
main.cpp:20:25: error: no matching function for call to ‘Json::Value<std::vector<unsigned int> >::Value(std::vector<unsigned int>&)’
Обратите внимание, что вопрос в том, почему компилятор выбирает
template <typename T> Json::Value<T> toJson(T x);
над
template <typename T> Json::Value< std::vector<T> > toJson(std::vector<T> const& rh);
Афаик, он должен выбрать последний, так как он более специализированный.
Спасибо!