C++核心准则​T.46:要求模板参数最少是正规或半正规的

2020-09-10 10:44:54 浏览数 (2)

T.46: Require template arguments to be at least Regular or SemiRegular

T.46:要求模板参数最少是正规或半正规的

Reason(原因)

Readability. Preventing surprises and errors. Most uses support that anyway.

可读性。防止意外的代码和错误。反正很多用法已经支持这一点。

Example(示例)

代码语言:javascript复制
class X {
public:
    explicit X(int);
    X(const X&);            // copy
    X operator=(const X&);
    X(X&&) noexcept;                 // move
    X& operator=(X&&) noexcept;
    ~X();
    // ... no more constructors ...
};

X x {1};    // fine
X y = x;      // fine
std::vector<X> v(10); // error: no default constructor
Note(注意)

Semiregular requires default constructible.

半正规要求默认可构造。

Enforcement(实施建议)

  • Flag types that are not at least SemiRegular.
  • 标记连半正规都没有实现的类型。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t46-require-template-arguments-to-be-at-least-regular-or-semiregular

0 人点赞