how to write a template converts vector to Json::Value (jsoncpp)

前端 未结 1 1128
天涯浪人
天涯浪人 2021-01-19 06:34

i wrote a template(like below) but it fails to compile

template class iterable>
Json::Value iterable2json(const itera         


        
相关标签:
1条回答
  • 2021-01-19 07:17

    The problem is that compiler can't deduce the type t as it is indirectly determined through the template template parameter. However, there is actually no need to do anything like that in the first place! The following works just fine:

    template <typename Iterable>
    Json::Value iterable2json(Iterable const& cont) {
        Json::Value v;
        for (auto&& element: cont) {
            v.append(element);
         }
         return v;
    }
    

    (well, since I don't have the Json library you are using I havn't tried to compile it but I'd think it should be fine).

    0 讨论(0)
提交回复
热议问题