关于“完美匹配”的混乱和“与次要调整”的MatchB&quot在完美的转发CTOR与其他构造函数的背景下
我正在阅读第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节。
- 完美匹配。该参数具有表达式的类型,或者具有对表达式类型的引用类型(可能带有添加
const
和/或volatile
pegifiers)。- 与次要调整匹配。例如,其中包括一个数组变量与指向其第一个元素的指针或添加
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 notconst
, which means thattemplate<typename HH, typename ...TT> Tuple(HH&&, TT&& ...)
is a perfect match for the call, whereastemplate<typename HH, typename ...TT> Tuple(Tuple<HH, TT...> const&)
requies thatconst
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:
- 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/orvolatile
qualifiers).- 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 typeint**
to a parameter of typeint 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论