jeudi 29 janvier 2015

constexpr static member vs variable

I stumbled upon some C++11 code that looks like this:



// some_file.h
namespace blah {
class X {
public:
constexpr const static std::initializer_list<uint64> SOME_LIST =
{1,2,3};
};
}

// some_file.cpp
#include "some_file.h"
namespace blah {
constexpr const X::SOME_LIST;
}


Which compiles fine. I assume the definition in the cpp file is used to avoid symbol duplication in each file that includes the header (please correct me if I'm wrong).


I then tried the following:



// my_file.h
namespace bleh {
constexpr const static char SOME_CONSTANT[] = "yay";
}

// my_file.cpp
#include "my_file.h"
namespace bleh {
// if I add this or any other variation, compilation breaks!
//constexpr const static char SOME_CONSTANT[];
}


The code above does not work if I add an explicit definition in the .cpp file. So I am wondering: is there symbol duplication in the second case? If so, is there a way to define the variable without an enclosing class?


Aucun commentaire:

Enregistrer un commentaire