Not a great solution, I suppose, but...
If your compiler activate all warnings (-Wall
for g++ and clang++, by example), you can substitute the #warning
row with something that generate a warning.
By example, an unused (maybe with a talking name) variable.
I've tried with
template <typename State>
void callStateFunction(const State& state) {
if constexpr (false) {
state.method();
} else {
int method_not_implemented[sizeof(State)];
}
}
and calling with a non-method value (callStateFunction(1)
, by example), I get
prog.cc: In instantiation of 'void callStateFunction(const State&) [with State = int]':
prog.cc:13:23: required from here
prog.cc:7:9: warning: unused variable 'method_not_implemented' [-Wunused-variable]
7 | int method_not_implemented[sizeof(State)];
| ^~~~~~~~~~~~~~~~~~~~~~
from g++ (head 11.0.0) and
prog.cc:7:9: warning: unused variable 'method_not_implemented' [-Wunused-variable]
int method_not_implemented[sizeof(State)];
^
prog.cc:13:4: note: in instantiation of function template specialization 'callStateFunction<int>' requested here
callStateFunction(1);
^
from clang++ (head 11.0.0)
I suggest that the unused variable depends from the template typename (State
) otherwise, if I define a non-dependent variable as
int method_not_implement;
I get a warning from clang++
prog.cc:7:9: warning: unused variable 'method_not_implemented' [-Wunused-variable]
int method_not_implemented;
^
also without calling the function with a non-method object.