3

In my C++ library, I have a function that is still there, 1) for debugging 2) for small operations. The function is basically a very slow fallback of more efficient versions. (think of a loop of individual assignments vs memcpy for example)>

For this reason, I would like to emit a warning as soon the function is invoked instantiated directly or indirectly. Without a warning, it is not easy to test if the function is being invoked instantiated because the function might be called instantiated through several layers of template code.

I found that GCC's __attribute__((warning("slow function!"))) does the job quite well.

template<class T> 
__attribute__((warning("careful this fun is very slow, redesign your algorithm"))) 
void slow_function(T){...}

However, it is not standard or compatible with clang.

Is there a better alternative for this kind of compile-time warning?

It looks like there is a standard [[deprecated("msg")]] attribute that also does the job, the problem is that it is confusing because there is nothing deprecated about this function, it is there for convenience.

There is also, I found recently, a #pragma poison that might be applicable here, but I don't understand how it is used, besides the function is actually a member function of a template class, the examples do not consider this case. https://www.fluentcpp.com/2018/09/04/function-poisoning-in-cpp/

alfC
  • 14,261
  • 4
  • 67
  • 118
  • By your explanation, this is not a "compile-time warning" at all. You want it emitted, not when the function *could* be called, but when it is *being called*. You said "*I would like to emit a warning as soon the function is invoked directly or indirectly*". Invoking a function is a runtime operation, not a compile-time one (unless the function is being invoked *at* compile-time, but let's not quibble). So, do you want a compile-time warning or message because there is a possible control path that leads to the function being called? Or should a runtime message get emitted when it gets called? – Nicol Bolas Aug 25 '19 at 03:32
  • @NicolBolas, you are right, I used the wrong word. I want to produce a warning when a certain member function of a template class is instantiated in the code. I changed my post. – alfC Aug 25 '19 at 03:34
  • Maybe something like `[[deprecated(...)]]`? – user975989 Aug 25 '19 at 20:15
  • @user975989, yes, as I siad, that is what I a using now, the problem is that there is nothing deprecated about this function, which can be confusing. I am d `[[deprecated("this is not deprecated, this is a warning: slow function, redesign your algorithm")]]` – alfC Aug 25 '19 at 20:46
  • 1
    I think by "slow function, redesign your algorithm" you are effectively *deprecating* (literally "expressing disapproval of") the function, so even if you aren't technically "deprecating" it, it makes sense to use `[[deprecated]]`. Anyway, your purpose is to inform the user that this function should not be used, so why not :) – L. F. Sep 12 '19 at 10:45
  • @L.F., This is a good point of view! As usual the compiler ends up convincing me that what I want to do is what the compiler already does. So, what I do now is `[[deprecated("slow function, redesign your algorithm")]]`. – alfC Sep 12 '19 at 19:56

0 Answers0