C++ Logo

std-discussion

Advanced search

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

From: Nicholl, Ryan <Ryan.Nicholl_at_[hidden]>
Date: Mon, 16 Sep 2019 16:00:47 +0000
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();
}

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.

Received on 2019-09-16 11:02:57