0

I'm trying to expose a function to the compiler based on the version info.

I have this version info.

#define LUA_VERSION_NUM     503

And I want to include the following function only when the version is equal to, or smaller than 501

static void lua_len(lua_State *L, int i) 
{
    //do something
}

How is this possible in C++?

Zack Lee
  • 2,784
  • 6
  • 35
  • 77

1 Answers1

2
#if LUA_VERSION_NUM <= 501
static void lua_len(lua_State *L, int i) 
{
    //do something
}
#endif

You might want to provide an empty lua_len for versions above 501 to prevent compilation errors:

#if LUA_VERSION_NUM <= 501
static void lua_len(lua_State *L, int i) 
{
    //do something
}
#else
static void lua_len(lua_State *L, int i) {}
#endif
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • 1
    For versions above Lua 5.1 the function `lua_len` is available, but you couldn't have known since the questions is not tagged [[tag:lua]]. – Henri Menke Jul 15 '18 at 21:40
  • @HenriMenke What do you mean? Do you think I don't need to do this? https://stackoverflow.com/questions/51349885/how-to-check-lua-version-in-swig-interface-file – Zack Lee Jul 16 '18 at 01:53
  • 1
    @ZackLee You don't need the `#else` branch, because Lua 5.2 and up provide `lua_len`. – Henri Menke Jul 16 '18 at 03:01
  • Oh, I know that. Thanks for your confirmation. – Zack Lee Jul 16 '18 at 03:41