3

I'm using a tuple as a key to track elements in a map, and then later iterating over the map to produce a string version of the map. To help me with the conversion, I have a template convenience function that will concatenate the tuple used as the 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 elements into the map and iterate 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 substitution 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;
}

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

$ 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 it says in the error message, the template I want it to use was ignored due to substitution failure. What difference did I introduce when I moved it to the map, and how would I correct it?

4

1 回答 1

4

cat_tuple将您的两个函数模板更改为

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

这是必要的,因为std::map的键是const

现场演示

在任何情况下,cat_tupletuple参数都应该是const&,因为它不会修改参数。


您可能想要重命名cat_tupletuple_printer或类似的名称并将std::stringstream参数更改为std::ostream,这样您可以std::cout在需要时将其传递给输出stdout

此外,现在的名字太让人联想到std::tuple_cat.

于 2015-04-27T23:13:14.250 回答