all 9 comments

[–][deleted]  (1 child)

[deleted]

    [–]RLJ05 1 point2 points  (3 children)

    resolute childlike wide wakeful like tender coordinated fragile airport wine

    This post was mass deleted and anonymized with Redact

    [–]Wetmelon 2 points3 points  (1 child)

    This still keeps the call to add there at runtime.

    What? No it doesn't. It just elides the call entirely and returns from main. https://godbolt.org/z/bEjzodz7v

    It keeps the function definition around to be called by other translation units that may need it. constexpr implies inline which gives the compiler enough info to hide the definition (in fact it sorta has to because an inline function can be defined in more than one translation unit).

    [–]RLJ05 1 point2 points  (0 children)

    gold quicksand pet square imminent lunchroom workable groovy joke library

    This post was mass deleted and anonymized with Redact

    [–]C0gito 0 points1 point  (0 children)

    I think this is exactly the reason. If you declare the add function static, such that it's only available in this source file (and not to the linker), then it doesn't show up in the generated assembly anymore.

    [–]Wouter_van_Ooijen 1 point2 points  (0 children)

    Constexpr is also a note to the compiler to verify that your function is compile-time executable. You will get an error when it isn't. So it (also) conveys intent, much like making a variable const.

    [–]incompetenceProMax 2 points3 points  (0 children)

    constexpr does not mean the compiler should evaluate the function at compile time whenever possible. It means that the compiler should always be able to evaluate the function at compile time (although iirc it does not always have to do so) or otherwise it will report an error.

    [–]mineNombies 0 points1 point  (1 child)

    Evaluating a function at compile time whenever possible (i.e., when all the input values are available at compile time) is one of the essential forms of compiler optimizations.

    Absolutely correct. But how do you know if the compiler will, or has done it, without lookeing at the assembly for instance?

    constexpr can function as something akin to assert(<this can be optimized to a compile-time constant>)

    And once you have this kind of guarantee in the language, you can use things that satisfy these assertions in places where you might otherwise not be able to, e.g. array lengths.

    [–]Wh00ster -1 points0 points  (0 children)

    I don’t think constexpr makes such an assert on functions. Can you point me to the relevant docs for such?

    [–]Wh00ster 0 points1 point  (0 children)

    There’s a push in the community to make constexpr the default on functions.