For many years, I have waited for function aliases in C++. They still aren't here. I've been using macros to generate wrapper functions. This is my best attempt at the perfect function alias:
#define FUN_ALIAS(NEW_NAME, ...) \
template <typename... Args> \
inline decltype(auto) NEW_NAME(Args &&... args) \
noexcept(noexcept(__VA_ARGS__(std::forward<Args>(args)...))) { \
return __VA_ARGS__(std::forward<Args>(args)...); \
}
I'm using std::forward
to forward the arguments and I'm using decltype(auto)
to get the right return type. In the future, we might have noexcept(auto)
which will be perfect for this situation but I'm not getting my hopes up.
Here's an example from my OpenGL wrapper:
template <GLenum TYPE>
Shader<TYPE> makeShader();
template <GLenum TYPE>
Shader<TYPE> makeShader(const GLchar *, size_t);
template <GLenum TYPE, size_t... SIZES>
Shader<TYPE> makeShader(const GLchar (&... sources)[SIZES]);
FUN_ALIAS(makeVertShader, makeShader<GL_VERTEX_SHADER>)
FUN_ALIAS(makeFragShader, makeShader<GL_FRAGMENT_SHADER>)
What I want to know:
- Is there a situation where the compiler will not optimize away the wrapper because the wrapper and the original function have different semantics?
- Does this fail to compile for some functions?
FUN_ALIAS_PTR
! I'm trying to think of a reason why it should exist but I've come up short. I think I'll remove it. \$\endgroup\$#define makeVertShader makeShader<GL_VERTEX_SHADER>
? \$\endgroup\$