Here is the trivial example of code I want to write:
#include <stdexcept>
#include <iostream>
template<typename T>
T test() {
static_assert(false, "test not implemented for type T");
// throw std::runtime_error("test not implemented for type T");
}
template<>
int test<int>() {
return 5;
}
int main() {
std::cout << test<int>() << std::endl;
return 0;
}
Even though the template function of type T is never required by the program (only the specialised template is), the compiler does not like this:
main.cpp: In function ‘T test()’:
main.cpp:14:19: error: static assertion failed: test not implemented for type T
14 | static_assert(false, "test not implemented for type T");
Is there a good reason something like this isn't possible?
I would love to have compile time rather than runtime implementation checks. If I switch to the runtime error, the code works as intended.