错误:'const std::array<int, 3u>&'不是类、结构或联合类型

error: 'const std::array<int, 3u>&' is not a class, struct, or union type

本文关键字:结构 类型 array std const lt int 3u 错误 gt      更新时间:2023-10-16

我正在尝试为大多数连接器进行转储,但是当我将连接器放入地图的值中时,我遇到了问题。 有我的代码:

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

错误消息是:

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, " "));

你能帮我找到为什么它不适用于这个,但它适用于我的主要所有其他测试吗?

更改为:

static void dump(T const& cont) {
  dumpSingle(cont);
}

现场演示

原因:decltype(cont)会导致T const&,而不是像您可能例外的那样T

我怀疑这是因为decltype(cont)是一种引用类型。 将第 59 行替换为

     dumpSingle<typename std::remove_reference<decltype(cont)>::type>(cont);

使代码为我编译,并且输出看起来合理。

更简单的是,你可以让编译器推断出要实例化dumpSingle()

     dumpSingle(cont);