错误:'const std::array<int, 3u>&'不是类、结构或联合类型
error: 'const std::array<int, 3u>&' is not a class, struct, or union type
我正在尝试为大多数连接器进行转储,但是当我将连接器放入地图的值中时,我遇到了问题。 有我的代码:
#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);
相关文章:
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- <badref> 当使用 CallInst::CreateMalloc 作为结构类型时
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 为什么"具有常量成员的结构"类型的指针不能指向"具有非常量成员的结构"?
- 查找声明为结构类型 C++ 的数组末尾
- C++ - 将结构类型的数组传递给用户定义的函数
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- NTTP 的结构类型:为什么所有成员都必须是公共的?
- 当我尝试访问结构类型时,我收到 std::bad_variant_access 异常
- 承诺可以使用结构类型吗?
- 填充模板化结构类型的结构成员
- 如何为结构类型变量赋值
- 在 c++ 中将结构类型转换为无符号字符*
- 在 c++ 中将一种结构类型分配给另一种类型
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- 确定输入"conscalc: calc"的类型时出错。不能联合编码器。具有不同字段集的结构类型
- 无法将结构类型的标识符设置为 nullptr;
- 结构类型成员的默认构造函数中的默认参数
- 如何在执行期间终止(或返回)具有结构类型的自动函数
- 在 stl 中迭代结构类型的队列