-2

I wonder if some C/C++ compilers implementing something similar to Pawn's stock keyword.

In Pawn, you can declare a function like that:

stock xfunc(a, b)
{
     [...] // Bla bla
}

The stock keyword tells the compiler to simply not include the function in the compiled binary if it's not called somewhere in the code. This makes the function facultative so to speak, and it won't increase the binary size if its not used.

I would find it useful to have something like this in C/C++ because I want to include some functions I will not immediately use in the first iterations of my program/code. Of course, some people might tell me there's other ways to do this like using preprocessor macros, etc etc. I'm not asking for another way, I want something that permits me to make use of those functions later without having to uncomment them, change a macro to make them get compiled, etc (i.e. seamlessly). BUT... without compiling them and thus increasing my executable size when I don't use them!

A handy feature I would say. Thanks!

P.S. If the answer is "it's not included in the language standards", are there any compilers that do it with specific pragmas, unofficial keywords, etc.?

EDIT: Should I mention, I'm mostly interested into this feature for virtual function. I'm aware function level linking exists for standard functions, but what about virtual ones? I think normally, if im not mistaken, virtual funcs get compiled even if not used to maintain class layout compatibe with a class prototype? Could you confirm? Thanks

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Yannick
  • 830
  • 7
  • 27
  • 6
    This is usually solved automatically during the linking phase, c or c++ doesn't need such keyword. – πάντα ῥεῖ Jul 10 '16 at 13:44
  • imho once I have written a function I want it to compile to catch compiler errors asap, thus I would never use macros or comments to exclude it from compilation as anyway it wont make it into the executable if it isnt used (as pointed out by others) – 463035818_is_not_an_ai Jul 10 '16 at 13:48
  • Virtually *all* compilers do it, seamlessly. This doesn't need to be part of the language standards. I don't see why the onus should be on the developer to annotate a function like this when the linker would be much better at making the decision. If you know something is going to be unused, just don't write it. – Cody Gray - on strike Jul 10 '16 at 14:04
  • See also: http://stackoverflow.com/questions/35648348/is-a-c-compiler-linker-allowed-to-remove-unused-methods, http://stackoverflow.com/questions/10809429/what-kind-of-dead-code-can-gcc-eliminate-from-the-final-output, http://stackoverflow.com/questions/17433791/eliminate-unused-virtual-functions, http://stackoverflow.com/questions/4813947/how-can-i-know-which-parts-in-the-code-are-never-used/, https://en.wikipedia.org/wiki/Dead_code_elimination – Cody Gray - on strike Jul 10 '16 at 14:05
  • Edited my question, about virtual funcs. – Yannick Jul 10 '16 at 14:34
  • @Yannick: C does not have virtual functions. C++ is a **different** language. Concentrate on **one** of them (I removed the C tag for this comment)! – too honest for this site Jul 10 '16 at 15:20
  • More interesting would it be to have something like a Pawn function *without* stock, issuing a warning if not used. – IS4 Jul 23 '16 at 14:16

1 Answers1

2

Any modern optimizing compiler/linker will do "dead code elimination" and strip out not just uncalled functions, but also unused bits of called functions.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Does this apply to a virtual function as well? Which is the main motive to my question (but somewhat forgot to mention it, now I did). – Yannick Jul 10 '16 at 14:31
  • 1
    As long as the compiler can see that it is unused. If you are in doubt, compile an exmple and look at the generated asm. – Jesper Juhl Jul 10 '16 at 16:22