[cfe-dev] why does non-const stateless tag value require global variable, but constexpr does not.

Arthur O'Dwyer via cfe-dev cfe-dev at lists.llvm.org
Mon Feb 4 13:30:59 PST 2019


On Mon, Feb 4, 2019 at 3:26 PM Larry Evans via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> This godbolt example code:
>
>    https://godbolt.org/z/tiZ4jZ
>
> shows that, depending on the value of #defined(TAG_CE),
> a global variable, i.e.:
>
>    tag_nc:
>          .zero   1
>
> is created.  Why can't the compiler see there's no need for this
> tag_nc global variable since the code behaves the same as when
> !defined(TAG_CE)?
>

The cfe-dev mailing list doesn't seem like the appropriate place for this
question; StackOverflow would be much better. [Moved the mailing list to
BCC.]

A definition is required for variable `tag_nc` because you wrote a
definition in your source code. The compiler needs to generate the symbol
for that definition, so that you'll be able to link this object file with
other object files that reference that symbol. For example, you might have
another translation unit that contained this C++ code:

    extern tag_t tag_nc;
    void foo() { tag_t *p = &tag_nc; }

If you don't want `tag_nc` visible outside of the current translation unit,
you should mark it `static` (or put it in an anonymous namespace), at which
point the compiler will see that no symbol is required because `tag_nc`
does not escape the translation unit.
https://godbolt.org/z/yQdPEg

–Arthur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190204/21fe2a69/attachment.html>


More information about the cfe-dev mailing list