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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

David Blaikie via cfe-dev
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)?

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: why does non-const stateless tag value require global variable, but constexpr does not.

David Blaikie via cfe-dev
On Mon, Feb 4, 2019 at 3:26 PM Larry Evans via cfe-dev <[hidden email]> 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.

–Arthur

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev