C++: Generating similar methods with macros
Do you consider this a good solution?


No.


  Would you have done it another way?


Yes.

Macros are never the solution. There is always a better tool. Macros are designed to deal with Hardware/OS/Compiler variations. You should limit their use to this situaation.

It looks like your macros are used to manually insert functions for specific situations. It seems like these could be deduced using template specializations. It is hard to provide a better solution without more code. But let my give it a go (this will probably be wrong as I don't have enough information).

template&lt;typename Type&gt;
TypeOfLuaObject LuaState::lua;

template&lt;typename Type&gt;
struct DoCheck;
template&lt;typename Type&gt;
struct DoConversion;

// Provide a specialization of these two macros for each
// type you are using.
template&lt;&gt;
struct DoCheck&lt;int&gt;
{
    bool operator()(TypeOfLuaObject&amp; lauObjec, int val) const {return lua_isnumber(lauObjec, val);}
};
template&lt;&gt;
struct DoConversion&lt;int&gt;
{
    int operator()(TypeOfLuaObject&amp; lauObjec, int val) const {return lua_tonumber(lauObjec, val);}
};

// The main function can then be left in its generalized form
// It will pick up the specialization of the above macros.
// Type will be deduced automatically from `def` parameter and the other two
// template parameters will be deduced from `Type` so you do not need to manually
// specify the type when calling.
template&lt;typename Type, typename Check=DoCheck&lt;Type&gt;, Conversion=DoConversion&lt;T&gt;&gt;
Type LuaState::getTableField(const char* table, const char* key, Type def)
{
    lua_getglobal(lua, table);
    if (!lua_istable(lua, -1))
    {   
        return def;
    }   
    Type result;
    lua_pushstring(lua, key);
    lua_gettable(lua, -2);
    Check doCheckObj;
    if (!checkObj(lua, -1))
    {   
        result = def;
    }   
    else
    {   
        Conversion doConversionObj;
        result = doConversionObj(lua, -1);
    }   
    lua_pop(lua, 1); 
    return result;
}


Other comments:

When you use macros. Make sure they are all uppercase. 

Macros do not obey scope boundaries. So we reserve all uppercase letters for macros to prevent accidental smashing of normal variables.

Don't use '_' as a prefix for identifiers.
Most people don't know the actual rules for this. So even if you do it will confuse them.

But you are breaking the rule with your macros _getGlobal and _getTableField. As they are macros they are not bound by the scope of your namespace and thus could potentially be trampling on some system macro.

See the artile I wrote on SO: https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797