I came across this question several times. Let's say I have a method:
template <typename T> bool Foo(T* param) {
If I pass in a non-pointer, Visual Studio will give me an error: could not deduce template argument for 'const T *' , but since the code is very dark, there is a ton of garbage attached to it (I say “garbage” because it is not connected to error ... i.e. namespaces, template types, etc.). Yes, I can say that it is not so by looking at the message, but it takes much more time, and I think that people can tell how it spends time in the long run.
So, I thought I would provide an overload and give a static statement:
template <typename T> bool Foo(T param) { STATIC_ASSERT_FORCE(Function_does_not_take_a_non_pointer_argument); }
This works well and I get a nice, clear error message. This function is placed immediately before the definition of the “correct” function, and it immediately becomes clear that I (or someone who uses my code) did wrong, and what needs to be done to fix it (that is, cause the correct overload).
But this clearly pollutes the list of methods. Is there any other way to output a better error message?
source share