I have some template functions & classes that when instantiated should fail compilation with given error message. I have used the following macro for it before and it has worked fine in Visual Studio & gcc:
#define PFC_CTF_ERROR(msg__) {struct cterror {char msg__:0;};}
Then if I have a template function I like to fail compilation upon instantiation I use it like this:
template<typename T>
void foo()
{
PFC_CTF_ERROR(you_should_never_compile_this_function);
}
However, now I'm porting my code to clang/llvm and it's failing compilation even when the function isn't instantiated. So I tried static_assert(false, "message");
instead, but that fails as well (now even in MSVC). An option I thought was to use an expression dependent on template argument like this:
#define PFC_CTF_ASSERT_MSG(e__, msg__) {struct cterror {char msg__:(e__);};}
template<typename T>
void foo()
{
PFC_CTF_ASSERT_MSG(sizeof(T)==0, you_should_never_compile_this_function);
}
And it works fine but is quite cumbersome to use. Does anyone have an idea how this could be done better and maintaining the old PFC_CTF_ERROR()
syntax?