Reputation: 934
I am working on a project, and it seems that clang is unable to generate a valid bytecode (as the linker fail to link, some static constexpr
in a template class is not found at link time)
I can fix it with a static getter in the class, but this lead to some really ugly/overloaded code.
Here is a "minimal" sample of code that makes the bug (is it a bug ?) appear. Unfortunately, here, g++ will produce the same link error.
I'm asking if this is a compiler bug, or I am simply doing something wrong, and if there's a solution to avoid this error. (And if I am doing something wrong, why the same construct works in my bigger project ??)
NOTE: The bigger project is named yaggler
on github, but is still at the early beginning of its life
#include <type_traits>
#include <iostream>
// exemple vector classes
struct vector2
{
constexpr vector2(unsigned int _x = 0, unsigned int _y = 0) : x(_x), y(_y) {} // for clang
unsigned int x;
unsigned int y;
};
struct vector3 : public vector2 // uh ;)
{
constexpr vector3(unsigned int _x = 0, unsigned int _y = 0, unsigned int _z = 0) : vector2(_x, _y), z(_z) {} // for clang
unsigned int z;
};
// simple templated generic vector type
// we could make a more generic one, but this would require something like a tuple.
template<unsigned int... Vals>
struct vector
{
static_assert(!(sizeof...(Vals) + 1), "[...]");
};
template<unsigned int X>
struct vector<X>
{
using vec_type = unsigned int;
static constexpr unsigned int value = X;
};
template<unsigned int X, unsigned int Y>
struct vector<X, Y>
{
using vec_type = vector2;
static constexpr vector2 value = vector2(X, Y);
};
template<unsigned int X, unsigned int Y, unsigned int Z>
struct vector<X, Y, Z>
{
using vec_type = vector3;
static constexpr vector3 value = vector3(X, Y, Z);
};
// a simple wrapper
template<typename V>
struct some_wrapper
{
static constexpr typename V::vec_type value = V::value;
};
// a dummy function that print something to stdout.
void do_something(int32_t id, const vector3 &value)
{
std::cout << id << " " << value.z << std::endl;
}
void do_something(int32_t id, const vector2 &value)
{
std::cout << id << " " << value.y << std::endl;
}
void do_something(int32_t id, int value)
{
std::cout << id << " " << value << std::endl;
}
// the class used to create the error
template< typename... Args>
class exemple
{
private:
// an initialisation that recurse over the Args... template arguments
template<typename Current, typename... Others>
void __rec_init() const
{
do_something(0, Current::value);
__rec_init<Others...>();
}
// end of recursion
template<size_t = 0>
void __rec_init() const {}
// launch the recursion
void tpl_init() const
{
__rec_init<Args...>();
}
public:
exemple()
{
tpl_init();
}
};
int main()
{
// and here, we get a linker error.
exemple<some_wrapper<vector<4, 4, 5>>, some_wrapper<vector<4, 1>>, some_wrapper<vector<9>>>();
}
EDIT: just to mention gcc and clang versions: gcc 4.7.3/4.8.2 and clang 3.2/3.3
Upvotes: 2
Views: 430
Reputation: 55395
The specializations of vector
class template for 2 and 3 template arguments have got static constexpr
data member value
of literal type (vector2
and vector3
, respectively) that don't have namespace scope definitions.
You'll need them because you odr-use value
when it binds to the reference parameter when passed to do_something
function.
§9.4.2/3 [class.static.mfct]
If a non-volatile
const static
data member is of integral or enumeration type, its declaration in the class definition can specify abrace-or-equal-initializer
in which everyinitializer-clause
that is anassignment- expression
is a constant expression (5.19). Astatic
data member of literal type can be declared in the class definition with theconstexpr
specifier; if so, its declaration shall specify abrace-or-equal-initializer
in which everyinitializer-clause
that is anassignment-expression
is a constant expression. [Note: In both these cases, the member may appear in constant expressions. —end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.
EDIT: Correcting myself, it's actualy some_wrapper<T>::value
that needs this definition (for the reason mentioned above, nonetheless). So what you need is this in namespace scope after the definition of some_wrapper
:
template<typename V>
constexpr typename V::vec_type some_wrapper<V>::value;
After that, your code compiles and runs.
Upvotes: 2