inline variable is initialized more than once

2117 views c++

Im seeing some examples of inline const variable getting initialized (and destructed) 3 times with visual studio 2017. Is this is a bug with the linker ? or is this supposed to happend in some other way ?

linker Comdat folding is set to Off.

Example Code:

#pragma once

struct A {
  A() {
    static int count = 0;
    ASSERT(count == 1);
  ~A() {

inline const A a = A();

In my solution, I have the assert fire twice (A constructor called 3 times). Inspecting the call stack shows all call stacks are identical and all calls come from dynamic initializer for a(). Now I know for a fact this class is not used in other parts of the solution since I just created it to investigate this issue.

answered question

Can we get a Minimal, Complete, and Verifiable example? Unless it is a compiler bug there should only be one instance created.

I suspect it could be bug in the linker (or compiler) yes

Doesn't happen with cl v19.16.26926

1 Answer


If this code was in a header that was included by 3 different translation units, then 3 copies of the variable a would exist, since const variables at namespace scope have implicit internal linkage. This would result in the constructor being called 3 times. You may be expecting the linker to merge together the 3 copies, but that would only happen if they had external linkage.

You can force a to have external linkage by adding the specifier extern.

This, incidentally, underscores the importance of providing an MCVE, as suggested in one of the comments. Without an MCVE, we can only guess what the issue might be.

posted this

Have an answer?


Please login first before posting an answer.