视觉 为什么C++不允许我使用typeof?

visual Why C++ doesn't allow me to use typeof?

本文关键字:typeof 允许我 为什么 C++ 不允许 视觉      更新时间:2023-10-16

我在c++ 11中使用以下代码,并得到一个错误,我不允许使用typeof !

问题是什么?如何解决?

错误:

Error   10  error C2923: 'typeof' is not a valid template type argument for parameter 'C'
下面是我的代码:
#define HIBERLITE_NVP(Field) hiberlite::sql_nvp< typeof(Field) >(#Field,Field)

class Person{
friend class hiberlite::access;
template<class Archive>
void hibernate(Archive & ar)
{
    ar & HIBERLITE_NVP(name); //ERROR
    ar & HIBERLITE_NVP(age);  //ERROR
    ar & HIBERLITE_NVP(bio);  //ERROR
}
public:
string name;
double age;
vector<string> bio;
};

sql_nvp如下:

template<class C>
 class sql_nvp{
public:
    std::string name;
    C& value;
    std::string search_key;
    sql_nvp(std::string _name, C& _value, std::string search="") :    name(_name), value(_value), search_key(search) {}
 };

您要找的是decltype():

#define HIBERLITE_NVP(Field) hiberlite::sql_nvp< decltype(Field) >(#Field,Field)
//                                               ^^^^^^^^

c++没有typeof操作符