C++ 错误:';常量std::数组<;国际,3u>&';不是类、结构或联合类型
我正在尝试为大多数内容转储,但当我在地图的值中放入内容时遇到了一个问题。这是我的代码:C++ 错误:';常量std::数组<;国际,3u>&';不是类、结构或联合类型,c++,c++11,C++,C++11,我正在尝试为大多数内容转储,但当我在地图的值中放入内容时遇到了一个问题。这是我的代码: #include <vector> #include <iterator> #include <iostream> #include <list> #include <string> #include <array> #include <map> template<typename T> struct has
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
模板
结构具有常量迭代器
{
私人:
typedef char是;
typedef结构{char数组[2];}否;
模板静态yes测试(typename C::const_iterator*);
模板静态无试验(…);
公众:
静态常量布尔值=sizeof(测试(0))==sizeof(是);
T型;
};
模板
结构具有\u开始\u结束
{
模板静态字符(&f)(typename std::enable_if<
std::is_same::value,void>::type*)[1];
模板静态字符(&f(…)[2];
模板静态字符(&g)(typename std::enable_if<
std::is_same::value,void>::type*)[1];
模板静态字符(&g(…)[2];
静态布尔常数beg_值=sizeof(f(0))==1;
静态布尔常数end_值=sizeof(g(0))==1;
};
模板
结构是容器:std::integral\u常量
{};
模板
无效转储单(T常量和续){
std::copy(cont.cbegin(),cont.cend(),std::ostream_迭代器(std::cout,“”);
std::cout更改为:
static void dump(T const& cont) {
dumpSingle(cont);
}
原因:decltype(cont)
导致T常量&
,而不是T
,如您所料。更改为:
static void dump(T const& cont) {
dumpSingle(cont);
}
原因:decltype(cont)
导致T常量&
,而不是T
,这可能是因为decltype(cont)
是一个引用类型。将第59行替换为
dumpSingle<typename std::remove_reference<decltype(cont)>::type>(cont);
我怀疑这是因为decltype(cont)
是一个引用类型
dumpSingle<typename std::remove_reference<decltype(cont)>::type>(cont);
它不是在std::array上做的,而是在数组的内容上做的。你可以在主数组的另一个数组中看到,它是有效的。一般来说,不必要地指定模板参数而不是使用类型推断可能会伤害你。如果有人能记住它是哪一个,STL在这方面做得很好。它不是在std::array上做的,而是在数组的内容上做的。你可以n在主数组的另一个数组中,它可以工作。一般来说,不必要地指定模板参数而不是使用类型推断可能会对您造成伤害。如果有人记得它是哪一个,STL在这方面做了很好的介绍。谢谢!您能解释一下为什么必须在这里删除模板吗?@Gabriedegrimouarddecltype()
正在强制查找严格接受常量引用的模板。@GabrieldeGrimouard此错误提供了信息,decltype(cont)=T常量&
而不是您所期望的T。谢谢!您能解释一下为什么必须在此处删除模板吗?@GabrieldeGrimouarddecltype()
正在强制查找严格接受常量引用的模板。@Gabriedegrimouard此错误是信息性错误,decltype(cont)=T常量&
而不是您所期望的T。