Nick
Nick

Reputation: 2682

Templated function can't be found, despite being declared

I have a code example here: http://codepad.org/9JhV7Fuu

With this code:

#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;
}

I'm getting this error:

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>&)’

Please note that the question is why the compiler picks

template <typename T> Json::Value<T> toJson(T x);

over

template <typename T> Json::Value< std::vector<T> > toJson(std::vector<T> const& rh);

Afaik it should pick the latter since it's more specialised.

Thanks!

Upvotes: 3

Views: 133

Answers (1)

Zharf
Zharf

Reputation: 2658

Well, as we know, you must declare the function before using it in C++, you declared

template <typename T>
Json::Value<T> toJson(T x) {
    return Json::Value<T>(x);
}

which matches with pretty much anything and then you started using it in the template <typename T, typename U> Json::Value< std::pair<T, U> > toJson(std::pair<T, U> const& rh) function.

What you missed here is that you declared the std::vector version after the version using std::pair, so it's not finding the correct function template to use.

To fix this, move the std::vector version to before the std::pair version or forward declare it.

Upvotes: 2

Related Questions