Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using following code with C++11 and getting a error that I'm not allowed to use typeof !

What is the problem and how to fix this ?

The error :

Error   10  error C2923: 'typeof' is not a valid template type argument for parameter 'C'

Here is my code :

#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 is like this :

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) {}
 };
like image 460
Mohsen Sarkar Avatar asked Apr 18 '13 10:04

Mohsen Sarkar


1 Answers

What you are looking for is decltype():

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

C++ does not have an operator called typeof.

like image 87
Andy Prowl Avatar answered Oct 11 '22 09:10

Andy Prowl