继 此答案,似乎这些构造函数:
template<class U, class V> pair(pair<U, V>&& p);
template<class U, class V> pair(const pair<U, V>& p);
在需要显式转换时被禁止参与重载决策。
从 C++11(第 20.3.2 节,n3290):
备注:此构造函数不应参与重载决策,除非 U 可以隐式转换为first_type 并且 V 可以隐式转换为second_type。
有人建议了一个有趣的 SFINAE 解决方法,但这偏离了标准的文本。
如果缺少一些特殊的内部编译器魔法,一致的实现如何可能将其排除在重载解析之外?即,一个实现可以做到这一点吗?我可以为我自己的类型复制它吗?似乎没有任何符合这一点的!这是从 C++11 中删除概念的后遗症吗?
我确实想知道是否使用私有构造函数来执行 SFINAE 部分并从公共构造函数进行委托,但构造函数委托似乎并没有以这样的方式参与 SFINAE 以使该工作正常进行。
Following on from this answer, it seems these constructors:
template<class U, class V> pair(pair<U, V>&& p);
template<class U, class V> pair(const pair<U, V>& p);
are forbidden from participating in overload resolution when they would require an explicit conversion.
From C++11 (§20.3.2, n3290):
Remark: This constructor shall not participate in overload resolution unless U is implicitly convertible to first_type and V is implicitly convertible to second_type.
An interesting SFINAE workaround has been suggested, but this digresses from the text of the standard.
How can a conforming implementation possibly exclude this from overload resolution, short of some special internal compiler magic? I.e. can an implementation do this and can I duplicate it for my own type perhaps? There doesn't seem to be anyway of conforming with this! Is it a hangover from the removal of concepts from C++11?
I did wonder about using a private constructor to do the SFINAE part and delegating from the public constructor, but it doesn't look like constructor delegation participates in SFINAE in such a way as to make that work.
发布评论
评论(2)
允许实现向任何非虚拟库成员函数添加默认参数。这似乎恰恰允许这种 SFINAE 技巧。
An implementation is allowed to add default arguments to any non-virtual library member function. This seems to permit precisely this kind of SFINAE tricks.
我缺少两条信息:
gcc 使用此:
这个技巧似乎存在于匿名
class
模板参数的默认值中。我之前没有见过这个实现,这个实现 没有使用它。I was missing two pieces of information:
gcc uses this:
The trick seems to be in the default for the anonymous
class
template parameter. I'd not seen that before and this implementation didn't use that.