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

时间:2022-07-24
本文章向大家介绍C++核心准则​T.46:要求模板参数最少是正规或半正规的,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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(示例)

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