a.hpp:
#pragma once
struct S
{
static int v;
};
int S::v = 0;
b.hpp:
#pragma once
void addOne();
b.cpp:
#include "b.hpp"
#include "a.hpp"
void addOne()
{
S::v += 1;
}
main.cpp:
#include <iostream>
#include "a.hpp"
#include "b.hpp"
int main()
{
S::v = 2;
addOne();
S::v += 2;
std::cout << S::v << std::endl;
}
Does not work when compiling with g++ -std=c++14 main.cpp b.cpp && ./a.out
(multiple definition of S::v).
However when I change the code to: a.hpp:
#pragma once
struct S
{
template<typename T>
static int v;
};
template<typename T>
int S::v = 0;
and replace all S::v
with S::v<void>
it compiles and works how I intended the first example to work (outputs 5
).
I believe I know why the first code example does not work: The int S::v = 0;
line gets once compiled in the main.cpp
unit and once in the b.cpp
unit. When the linker links these two together, then the variable S::v
gets essentially redefined.(?)
Why does the code with the template work?