vendredi 14 février 2020

GCC link error: undefined reference of static constexpr int member

I have a struct with a static constexpr integer member in a header that fails at link time when compiling on gcc 6.3.1 using std=cpp11

for example if I have

namespace SomeNamespace
{
namespace detail
{

struct Type
{

    constexpr Type(const uint64_t passedID, const char * passedTypeName) :
        typeID(passedID),
        typeName(passedTypeName)
    {
    };
    ~Type() {}

    std::string name() { return typeName; };

    const uint64_t typeID;
    const char * typeName;
};

template<typename T>
struct TypeAllocator;

template<typename T>
struct TypeAdaptor: public Type
{
    constexpr TypeAdaptor() {};

    enum
    {
        Defined = false
    };
}

template<>
struct TypeAdaptor<bool> : public Type
{
    constexpr static const int typeID = 5; //This is actually a generated constexpr hash 

    constexpr static const char * const typeName = "bool"; 

    constexpr TypeAdaptor(): Type(TypeAdaptor<bool>::typeID, typeName) {} 
};
}
}

and then refer to it later as

//In SomeNamespace
int someint = detail::TypeAdaptor<bool>::typeID ;

Intellisense knows what the value is, but when it comes to link time I get:

[build] ../project/libName-d.so: undefined reference to `SomeNamespace::detail::TypeAdaptor<bool>::typeID'

Which to me makes no sense that intellisense can immediately tell me what the value is at the same location of usage.

What becomes even stranger is if I replace

constexpr static const int typeID = 5; 
//replaced with 
constexpr static const char * typeID = "something"; 

it has no problems with it.

Aucun commentaire:

Enregistrer un commentaire