I have the following code:
// template_header.hpp
#ifndef TEMPLATE_HEADER_HPP
#define TEMPLATE_HEADER_HPP
namespace template_header
{
template <int dim1>
/*static*/ constexpr int dim2 = 0;
template <>
/*static*/ constexpr int dim2<2> = 3;
template <>
/*static*/ constexpr int dim2<3> = 5;
}
#endif
// lib1.cpp
#include <array>
#include "template_header.hpp"
template <int dim1>
class lib1_class
{
public:
std::array< double, template_header::dim2<dim1> > ar1 = {0};
};
// lib2.cpp
#include <array>
#include "template_header.hpp"
template <int dim1>
class lib1_class
{
public:
std::array< double, template_header::dim2<dim1> > ar1 = {0};
};
If I compile any of the .cpp
files with static
uncommented, GCC gives me an "explicit template specialization cannot have a a storage class" error.
If static
is commented, I can compile both .cpp
files and then link them together as a shared library with g++ lib1.o lib2.o -shared -o shared_lib.so
.
However, if I compile with static
commented out with clang, I get no problems during compilation, but I get a "multiple definition of template_header::dim2<2>'" error during linking. If I uncomment
static`, then everything compiles and links fine.
I'm pretty confused about this, firstly given that this answer indicates that, since my constexpr
's happen in a namespace scope, they ought to automatically be static
and therefore should pose no problem for the linker even if static
is commented out.
Additionally, I don't understand why adding static
beforehand would change how GCC compiles the .cpp
files, given that it should be implicitly static.
Any explanation of the errors + possible fixes are appreciated.
Edit: I am using C++14.