Is there a situation where the compiler will not optimize away the wrapper because the wrapper and the original function have different semantics?
Yes to the first part and no to the second. Any optimizing compiler will inline your wrapper, as it consists of only one return statement.
But on the other hand, the original function might have different semantics: It might be constexpr
for example, and your alias will not be.
Does this fail to compile for some functions?
No (except for the constexpr
part).
So here are some improvements you can use:
Do you really need
__VA_ARGS__
? It looks likeFUN_ALIAS
is only supposed to be called with two arguments; if I'm not missing something. But at least it's more than one.Templates are implicitly
inline
, so you don't need to write it explicitly.Use a lambda to combat the missing
constexpr
ness of the alias, because since C++17 lambdas are implicitlyconstexpr
if they can be.
This will get you:
#define FUN_ALIAS(NEW_NAME, OLD_NAME...) \
inline auto NEW_NAME = [](auto &&... args) \
noexcept(noexcept(OLD_NAME__VA_ARGS__(std::forward<decltype(args)>(args)...))) \
-> decltype(auto) { \
return OLD_NAME__VA_ARGS__(std::forward<decltype(args)>(args)...); \
};