下面这个定义在我的编译器中不被允许,因为std::string
有一个非平凡的析构函数(当成员变量存在时,teststr
无法拥有平凡的析构函数是有意义的):
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; }
};
然而,根据我所知,以下等效的teststr
定义是允许的:
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;
什么是模板类型定义的允许条件?