1

I have a function I will need to leave partially implemented for a variety of reasons and I want to prevent future users (read as me in the future when I have forgotten that I did this) to know the function is incomplete, buggy and untested.

Option n1 is merely adding a comment // Warning this thing is partially implemented and will break randomly This however won't create compile time warnings so, I am not a fan.

Option n2 is to use [[deprecated("reason")]] which has the advantage of raising compile warnings but its misleading, the function wasn't deprecated it's actually the opposite of deprecation, it's a WIP and will perhaps one day be fully implemented.

Are there alternatives?

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • Actually, `[[deprecated("this function is buggy, and will need fixing")]]` doesn't seem terrible. Or even `[[deprecated("not really, just incomplete :)")]]` – cigien Aug 04 '20 at 00:40
  • I know this is a language question, but as a general sw practice it is bad idea to have half working code commited. I would just implement some basic functionality correctly and document the limitations. – NoSenseEtAl Aug 04 '20 at 00:57
  • This is research code, it's often not possible to not have half working code you leave aside. You don't want to lose all the work you put into the prototype but you also don;t want to keep spending valuable time in a hard to implement algorithm if you think you;ve found a new lead. – Makogan Aug 04 '20 at 01:25
  • 1
    If it's research code, it sounds like you really want to use a code repository like GitHub and just branch off on the experimental code that may or may not work. If it does work, merge it, if it doesn't, stop work on that branch in order to keep the code around or delete the branch. – Casey Aug 04 '20 at 02:17
  • This creates the problem that as time passes and you refactor the code merging can become a nightmare. If the prototype remains in the code and I change a dependency that break the current functionality, my tests will catch it and I can fix it as I go, which is exponentially less difficult than fixing everything at once if I ever need to get back to it – Makogan Aug 04 '20 at 02:20

2 Answers2

1

The [[deprecated]] attribute is exactly what this is for (emphasis mine) :

https://en.cppreference.com/w/cpp/language/attributes

[deprecated]

[deprecated("reason")]

indicates that the use of the name or entity declared with this attribute is allowed, but discouraged for some reason

You can still use the function, you just get a warning message that you shouldn't rely on its use.

Caveat: MSVC breaks the standard and emits a compiler error (due to SDL flag being turned on by default) instead of a warning.

Casey
  • 10,297
  • 11
  • 59
  • 88
  • It seems like a warning in [msdn](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=vs-2019). – Louis Go Aug 04 '20 at 01:08
  • The trouble is thatifyou use warnings-as-errors then it will force the compilation to fail, if you use #pragma warning(disable:4996) then you won't get the message at all. – SoronelHaetir Aug 04 '20 at 01:16
  • @SoronelHaetir It's part of the /SDL flag that the VS team has refused to fix for years: 1) https://stackoverflow.com/questions/20448102/why-does-visual-studio-2013-error-on-c4996 2) https://developercommunity.visualstudio.com/content/problem/760394/compiler-bug-deprecated-attribute-results-in-error.html 3) https://developercommunity.visualstudio.com/content/problem/164550/c4996-shown-as-error-by-default.html 4) https://developercommunity.visualstudio.com/content/problem/786502/cant-treat-deprecated-warning-as-warning-with-wx.html – Casey Aug 04 '20 at 01:21
1

The only thing in standard C++ for this is the [[deprecated("message")]] attribute.

GNU has a non-standard Function Attribute for warning messages:

warning ("message")
If this attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, a warning which will include message will be diagnosed. This is useful for compile time checking, especially together with __builtin_constant_p and inline functions. While it is possible to define the function with a message in .gnu.warning* section, when using this attribute the problem will be diagnosed earlier and with exact location of the call even in presence of inline functions or when not emitting debugging information.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770