1

Is it possible to define a C++ class method that would compile cleanly but produce a warning message when used, in a way similar to the unimplemented C library functions? Something like:

int MyClass::frobnicate() 
{
    link_warning(frobnicate, "function not implemented");
    return 0;
}

An __attribute__((deprecated)) or a newer [[deprecated]] attribure has the right functionality (that's what I will use if there's no other option), but I would like to avoid telling the user that frobnicate is deprecated when it's really just cannot be implemented due to XYZ reason.

A C code example (from glibc):

#define link_warning(symbol, msg) \
  __make_section_unallocated (".gnu.warning." #symbol) \
  static const char __evoke_link_warning_##symbol[]     \
  __attribute__ ((used, section (".gnu.warning." #symbol __sec_comment))) \
  = msg;

The warning this macro gives looks like the following (here's an example of code using it):

/path/to/file.o: In function `symbol':

/path/to/file.c:line: warning: msg

A naive link_warning(MyClass::frobnicate, "function not implemented") doesn't seem to work, and finding out and using decorated names seems hacky and unportable (and I'm not sure it will actually work).

Community
  • 1
  • 1
Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
  • What "unimplemented C library functions" are you referring to? Any reason not to simply print a message to stderr (e.g. via `std::cerr`) or better yet, throw an exception? What are you trying to accomplish? – TypeIA Jan 20 '18 at 22:38
  • What is linker warning? Do you mean linker error? – hyde Jan 20 '18 at 22:41
  • 2
    IIRC using `gets` triggers a linker warning on Linux. – melpomene Jan 20 '18 at 22:43
  • @hyde I mean something similar to what you get when you try to call [`lchmod()` on Linux](https://lists.gnu.org/archive/html/bug-coreutils/2009-09/msg00268.html). – Dmitry Grigoryev Jan 20 '18 at 22:48
  • 1
    You might want to add an example C code, and the warning it gives. – hyde Jan 22 '18 at 07:28
  • @hyde Hi and thanks for following up. I have added a code example. – Dmitry Grigoryev Jan 22 '18 at 08:04
  • 1
    I wonder if you could get somewhere with `extern "C"` and a proxy C function like `MyClass_frobnicate`... Maybe not, just a thought. – hyde Jan 22 '18 at 08:27
  • 1
    can't you just apply `__attribute((warning("message")))` or `[[gnu::warning("message")]]` to your function ? – Massimiliano Janes Jan 22 '18 at 08:35
  • @MassimilianoJanes Geez, I somehow missed those things. Makes me wonder why did glibc implement this in such a complex way if there's an easy way to it. Portability? – Dmitry Grigoryev Jan 22 '18 at 09:03
  • 1
    @DmitryGrigoryev the difference is that one works at compile time (survives inlining, but not dead code elimination...), the other at link time (won't fire when inlined...) ... – Massimiliano Janes Jan 22 '18 at 09:09

0 Answers0