关于“完美匹配”的混乱和“与次要调整”的MatchB&quot在完美的转发CTOR与其他构造函数的背景下

发布于 2025-01-25 08:54:16 字数 2113 浏览 1 评论 0原文

我正在阅读第25章的第25章 c ++模板 - 完整指南 - 第二版。,在哪里,在哪里给定代码,像这样不太这样

#include <utility>

template<typename...>
struct Tuple;

template<typename H, typename ...T>
struct Tuple<H, T...> {
    H h;
    Tuple<T...> t;
    Tuple() {}
    template<typename HH, typename ...TT>
    Tuple(HH&& h, TT&& ...t)
        : h{std::forward<HH>(h)}
        , t{std::forward<TT>(t)...} {}
    template<typename HH, typename ...TT>
    Tuple(Tuple<HH, TT...> const& other)
        : h{other.h}
        , t{other.t} {}
};

template<>
struct Tuple<> {};

t这样定义了

Tuple<int, double, std::string> const t;

以下定义失败

Tuple<long int, long double, std::string> t2{t};

,好,我知道为什么会发生这种情况,我开始写入保证金注:

原因是t不是const,这意味着template&ltplate&typename hh,typename ... tt&gt;元组(HH&amp;&amp; tt&amp;&amp; ...)是通话的完美匹配,而template&lt; typeName HH,TypeName ... tt&gt;元组(元组&lt; hh,tt ...&gt; const&amp;)要求const添加到参数中以匹配参数。

并且我已经验证了它:如果我制作t const,那么t2的定义很好,调用正确的ctor。

但是(假设我不添加const根据上一段)我想知道“第二种类型是第二类,如果第一个是 perfect ?”

我的几个非完美匹配中的哪个是我的两倍,所以我向附录C,第682-683页,第2节。

  1. 完美匹配。该参数具有表达式的类型,或者具有对表达式类型的引用类型(可能带有添加const和/或volatile pegifiers)。
  2. 与次要调整匹配。例如,其中包括一个数组变量与指向其第一个元素的指针或添加const的dacy,以将类型int **的参数匹配到参数类型int const* const*

现在我有些困惑,因为ctor template&lt; typename hh,typename ... tt&gt;元组(元组&lt; hh,tt ...&gt; const&amp;其他),引用t2的定义,具有一个参数,具有对表达式初始化类型的类型引用它由于模板类型的扣除,只有添加cosnt。那么,这不是根据本书的摘录而完美匹配?还是我正在误读这本书?还是我自己的代码?

I was reading Chapter 25 from C++ Templates - The Complete Guide - 2nd ed., where, given code more or less like this

#include <utility>

template<typename...>
struct Tuple;

template<typename H, typename ...T>
struct Tuple<H, T...> {
    H h;
    Tuple<T...> t;
    Tuple() {}
    template<typename HH, typename ...TT>
    Tuple(HH&& h, TT&& ...t)
        : h{std::forward<HH>(h)}
        , t{std::forward<TT>(t)...} {}
    template<typename HH, typename ...TT>
    Tuple(Tuple<HH, TT...> const& other)
        : h{other.h}
        , t{other.t} {}
};

template<>
struct Tuple<> {};

and a t defined like this

Tuple<int, double, std::string> const t;

the following definition fails

Tuple<long int, long double, std::string> t2{t};

Ok, good, I knew why that was happening and I started writing a margin note:

The reason is that t is not const, which means that template<typename HH, typename ...TT> Tuple(HH&&, TT&& ...) is a perfect match for the call, whereas template<typename HH, typename ...TT> Tuple(Tuple<HH, TT...> const&) requies that const is added to the argument to match the parameter.

And I've verified it: if I make t const, then the definition of t2 is fine an calls the correct ctor.

But then (assuming I don't add the const as per previous paragraph) I wondered "What type of match is the second, if the first is perfect?"

I was in double as to which of the several non-perfect matches that would be, so I moved forward to Appendix C, pages 682-683, Section C.2 to find out:

  1. Perfect match. The parameter has the type of the expression, or it has a type that is a reference to the type of the expression (possibly with added const and/or volatile qualifiers).
  2. Match with minor adjustments. This includes, for example, the dacy of an array variable to a pointer to its first element or the addition of const to match an argument of type int** to a parameter of type int const* const*

And now I'm a bit puzzled, because the ctor template<typename HH, typename ...TT> Tuple(Tuple<HH, TT...> const& other), with reference to the definition of t2, has a parameter with type a reference to the type of the expression initializing it, because of template type deduction, with just an added cosnt. So isn't this exactly what a perfect match is, based on the excerpt from the book? Or am I misreading the book? Or my own code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文