vendredi 29 janvier 2016

error: 'const std::array

I am trying to make a dump for most of the conteners but I got a problem when I put put a contener in value of my map. there is my code:

#include <vector>
#include <iterator>
#include <iostream>
#include <list>
#include <string>
#include <array>
#include <map>



template<typename T>
struct has_const_iterator
{
private:
    typedef char                      yes;
    typedef struct { char array[2]; } no;

    template<typename C> static yes test(typename C::const_iterator*);
    template<typename C> static no  test(...);
public:
    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
    typedef T type;
};

template <typename T>
struct has_begin_end
{
    template<typename C> static char (&f(typename std::enable_if<
      std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::begin)),
      typename C::const_iterator(C::*)() const>::value, void>::type*))[1];

    template<typename C> static char (&f(...))[2];

    template<typename C> static char (&g(typename std::enable_if<
      std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::end)),
      typename C::const_iterator(C::*)() const>::value, void>::type*))[1];

    template<typename C> static char (&g(...))[2];

    static bool const beg_value = sizeof(f<T>(0)) == 1;
    static bool const end_value = sizeof(g<T>(0)) == 1;
};

template<typename T> 
struct is_container : std::integral_constant<bool, has_const_iterator<T>::value && has_begin_end<T>::beg_value && has_begin_end<T>::end_value> 
  {};


template<typename T>
void dumpSingle(T const& cont) {
    std::copy(cont.cbegin(), cont.cend(), std::ostream_iterator<typename T::value_type>(std::cout, " "));
    std::cout << "\n";
}

template <typename T, bool U>
class Dump {
 public:
     static void dump(T const& cont) {
        dumpSingle<decltype(cont)>(cont);
    }
};

template<typename T>
class Dump<T, false> {
public:
    static void dump(T const& val) {
        std::cout << val << "\n";
    }
};

template<typename T>
void dumpPair(T const& cont) {
    for (auto pair : cont) {
        std::cout << "[" << pair.first << "]: ";
        Dump<decltype(pair.second), is_container<decltype(pair.second)>::value>::dump(pair.second);
    }
}

int main(int argc, char const *argv[])
{
    std::vector<int> vec = {2, 4, 42, 0, 7};
    dumpSingle<decltype(vec)>(vec);

    std::array<int, 3> arr = {1, 7, 5};
    dumpSingle<decltype(arr)>(arr);

    std::list<std::string> l = {"toto", "tutu", "titi"};
    dumpSingle<decltype(l)>(l);

    std::map<std::string, int> map;

    map["yop"] = 42;
    map["test"] = 25;
    dumpPair<decltype(map)>(map);

    std::map<std::string, std::array<int, 3>> map2;

    map2["yop"] = {42, 258, 72};
    map2["test"] = {77, 42, 21};
    dumpPair<decltype(map2)>(map2); // error here

    return 0;
}

the error message is:

dump.cpp: In instantiation of 'void dumpSingle(const T&) [with T = const std::array<int, 3u>&]':
dump.cpp:59:34:   required from 'static void Dump<T, U>::dump(const T&) [with T = std::array<int, 3u>; bool U = true]'
dump.cpp:75:92:   required from 'void dumpPair(const T&) [with T = std::map<std::basic_string<char>, std::array<int, 3u> >]'
dump.cpp:100:31:   required from here
dump.cpp:51:101: error: 'const std::array<int, 3u>&' is not a class, struct, or union type
  std::copy(cont.cbegin(), cont.cend(), std::ostream_iterator<typename T::value_type>(std::cout, " "));

Can you help me to find why it's not working for this but it's work for all the others tests in my main ?

Aucun commentaire:

Enregistrer un commentaire