C++ Logo

std-discussion

Advanced search

Re: Is this a Visual Studio 2019 bug or Clang/GCC bug?

From: Brian Bi <bbi5291_at_[hidden]>
Date: Mon, 16 Sep 2019 11:12:19 -0500
On Mon, Sep 16, 2019 at 11:00 AM Nicholl, Ryan via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> I’m mailing to ask about what I assume is a bug in Visual Studio.
>
>
>
> Suppose we have a header like the following:
>
>
>
> #ifndef FOO_HPP
>
> #define FOO_HPP
>
>
>
> #include <iostream>
>
>
>
> void test_a();
>
> void test_b();
>
>
>
> class foo
>
> {
>
>
>
> public:
>
> static int & bar()
>
> {
>
> static int & baz;
>
> return baz;
>
> }
>
> };
>
>
>
> #endif
>
>
>
> Then two .cpp files like so:
>
>
>
> testa.cpp
>
>
>
> #include “test.hpp”
>
>
>
> void test_a()
>
> {
>
> std::cout << & foo::bar() << std::endl;
>
> }
>
>
>
> testb.cpp
>
>
>
> #include “test.hpp”
>
>
>
> void test_a()
>
> {
>
> std::cout << & foo::bar() << std::endl;
>
> }
>
>
>
> Plus a main file:
>
>
>
> test-main.cpp
>
>
>
> #include “test.hpp”
>
> int main()
>
> {
>
> test_a();
>
> test_b();
> }
>

That code shouldn't compile, since `baz` is a reference that lacks an
initializer.


>
>
> Each is compiled in a separate translation unit, then linked together for
> the final executable.
>
>
>
> GCC and Clang agree that only one baz exists.
>
> Visual Studio thinks that foo::bar() gets a separate copy of baz per
> translation unit.
>
>
>
> I believe that visual studio is bugged and GCC/clang behave correctly, as
> I assume the static member functions are implicitly inline and that the
> static keyword in class scope does not alter the visibility of the
> function. Thus the function should be inline and in case it would be an
> inline function scope static variable which I understand should not exist
> in counterparts.
>
>
>
> I would like an opinion from the experts here! Thanks.
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>


-- 
*Brian Bi*

Received on 2019-09-16 11:14:39