C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: Is this a Visual Studio 2019 bug or Clang/GCC bug?
From: Nicholl, Ryan (Ryan.Nicholl_at_[hidden])
Date: 2019-09-16 11:18:12


My bad, I was typing up a generified version and made a couple mistakes. It should be static int baz; and not static int & baz; Also, the “test_a” in testb.cpp is supposed to be “test_b”. Hope that clears it up.


From: Brian Bi <bbi5291_at_[hidden]>
Sent: Monday, September 16, 2019 12:12 PM
To: std-discussion_at_[hidden]
Cc: Nicholl, Ryan <Ryan.Nicholl_at_[hidden]>
Subject: Re: [std-discussion] Is this a Visual Studio 2019 bug or Clang/GCC bug?

*External Message* - Use caution before opening links or attachments



On Mon, Sep 16, 2019 at 11:00 AM Nicholl, Ryan via Std-Discussion <std-discussion_at_[hidden]<mailto: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]<mailto:Std-Discussion_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion$>


--
Brian Bi



STD-DISCUSSION list run by herb.sutter at gmail.com

Older Archives on Google Groups