C ++: using type as map key introduces template replacement errors

I use a tuple as a key to keep track of items in the map, and then iterate over the map to create a string version of the map. To help me with the transformation, I have a template convenience function that concatenates a tuple used as a key (inspired by this answer ).

#include <iostream>
#include <string>
#include <sstream>
#include <tuple>

template<std::size_t idx = 0, typename ...T>
inline typename std::enable_if<idx == sizeof...(T), void>::type
cat_tuple(std::tuple<T...> &t, std::stringstream &s){
}

template<std::size_t idx = 0, typename ...T>
inline typename std::enable_if<idx < sizeof...(T), void>::type
cat_tuple(std::tuple<T...> &t, std::stringstream &s){
    if (idx != 0)
        s << ",";
    s << std::get<idx>(t);
    cat_tuple<idx+1, T...>(t, s);
}

typedef std::tuple<int, int> my_tuple;
int main(){
    my_tuple t(1, 2);
    std::stringstream s;
    cat_tuple(t, s);
    std::cout << s.str() << std::endl; //Correctly prints "1,2"
}

      

I can insert items into a map and iterate over without error

#include <iostream>
#include <string>
#include <sstream>
#include <tuple>
#include <map>

typedef std::tuple<int, int> my_tuple;
typedef std::map<my_tuple, int> my_map;
int main(){
    my_map m;
    my_tuple t(1, 2);
    m.insert(std::pair<my_tuple, int>(t, 1));
    std::stringstream s;
    for(my_map::iterator i = m.begin(); i != m.end(); ++i)
        s << i->second;
    std::cout << s.str() << std::endl; //Correctly prints "1"
}

      

But when I try to iterate over the map, I get a replacement error:

#include <iostream>
#include <string>
#include <sstream>
#include <tuple>
#include <map>

template<std::size_t idx = 0, typename ...T>
inline typename std::enable_if<idx == sizeof...(T), void>::type
cat_tuple(std::tuple<T...> &t, std::stringstream &s){
}

template<std::size_t idx = 0, typename ...T>
inline typename std::enable_if<idx < sizeof...(T), void>::type
cat_tuple(std::tuple<T...> &t, std::stringstream &s){
    if (idx != 0)
        s << ",";
    s << std::get<idx>(t);
    cat_tuple<idx+1, T...>(t, s);
}

typedef std::tuple<int, int> my_tuple;
typedef std::map<my_tuple, int> my_map;
int main(){
    my_map m;
    my_tuple t(1, 2);
    m.insert(std::pair<my_tuple, int>(t, 1));
    std::stringstream s;
    for(my_map::iterator i = m.begin(); i != m.end(); ++i){
        if (i != m.begin())
            s << "\n";
        cat_tuple(i->first, s); //Substitution error, see below
        s << " " << i->second;
    }
    std::cout << s.str() << std::endl;
}

      

(g ++ 4.2.1 on OSX, removed extraneous notes enable_if)

$ g++ asdf.cc -std=c++11
asdf.cc:31:3: error: no matching function for call to 'cat_tuple'
                cat_tuple(i->first, s); //Substitution error, see below
                ^~~~~~~~~
...
asdf.cc:14:1: note: candidate template ignored: substitution failure [with idx =
      0, T = <int, int>]
cat_tuple(std::tuple<T...> &t, std::stringstream &s){
^
1 error generated.

      

As the error message says, the template I want to use has been ignored due to a replacement failure. What difference did I imagine when I moved it to the map, and how would I fix it?

+3


source to share


1 answer


Change both function templates cat_tuple

to

cat_tuple(std::tuple<T...> const& t, std::stringstream &s)
//                         ^^^^^

      

This is necessary because the keys are std::map

const

.

Live demo



The parameter cat_tuple

tuple

should be there anyway const&

, since it doesn't change the argument.


You can rename cat_tuple

to tuple_printer

or something like that and change the parameter std::stringstream

to std::ostream

so you can pass it std::cout

to the output if you like stdout

.

Also, the current name is too similar std::tuple_cat

.

+4


source







All Articles