dimanche 28 décembre 2014

Literal class with std::string only works with template specialization?

The following definition is not allowed by my compiler as std::string has a non trivial destructor (makes sense that teststr can't have a trivial dtor when a member doesn't):



class teststr
{
private:
std::string _m;
public:
constexpr teststr(std::string value) : _m(value) {};

constexpr std::string m() const { return _m; }
void m(std::string value) { _m = value; }
};


However, the following equivalent (to the best of my knowledge) definition of teststr is allowed:



template<typename T>
class test
{
private:
T _m;
public:
constexpr test(T value) : _m(value) {};

constexpr T m() const { return _m; }
void m(T value) { _m = value; }
};

typedef test<std::string> teststr;


What is it about templating the type that makes this definition allowed?


Aucun commentaire:

Enregistrer un commentaire