#include <iostream>
#include <typeinfo>
#include <map>
#include <stdlib.h>
using namespace std;
struct foo {
int one;
int i;
int s;
};
template<class myType>
void serialize(myType b, string &a, int epe) {
//three.resize(42);
int type = typeid(b).name()[35] == 'S' ? 1 : 0; // testing if the map(b) value weather a struct or an int
if (type) {
auto it = b.begin();
cout << sizeof(it->second) / sizeof(int) << endl;
cout << it->second.one;
} else {
cout << sizeof(b) / sizeof(int) << endl;
}
}
int main() {
cout << "Hello world!" << endl;
map<int, int> hey;
map<int, foo> heya {
{ 1, { 66 } },
};
typedef map<int, foo> mappy;
typedef map<int, int> happy;
string duck;
auto it = heya.begin();
serialize<happy>(hey, duck, 4);
serialize<mappy>(heya, duck, 4);
return 0;
}
所以我收到了这个错误,我认为这是因为它(map<int,int>)
在模板函数的一部分上使用 int 类型的值测试映射,即使我在使用之前尝试了专门化类型,也不应该达到它的 int 结构功能,还是不行。
serialize\main.cpp|36|error: request for member 'one' in 'it.std::_Rb_tree_iterator<_Tp>::operator->
[with _Tp = std::pair<const int, int>, std::_Rb_tree_iterator<_Tp>::pointer = std::pair<const int, int>*]()->std::pair<const int, int>::second',
which is of non-class type 'int'|
||=== Build finished: 1 errors, 1 warnings ===|