即使 V(U) 有效,从 std::pair<T, U> 到 std::pair<T, V> 的转换也不起作用?

Conversion from std::pair<T, U> to std::pair<T, V> doesn't work even if V(U) works?

这是一个适用于 C++03 (-std=c++03) 但在 GCC 和 VS2015 for C++11 (-std=c++11, /Qstd=c++) 上失败的示例11)

#include <utility>

class B {
public:
   B(float);
};

class A {
public:
   A(B);
};

std::pair<int, A> a(std::make_pair(1, 2.0));

我不知道为什么这会无效。据我所知,A 成员由 float 直接初始化,如 http://en.cppreference.com/w/cpp/utility/pair/pair 中所述。是否有针对隐式可转换性的 SFINAE 测试?据我所知,在 cppreference 上它没有提到类似的东西。

N3337 20.3

构造函数

template<class U, class V> pair(const pair<U, V>& p);

Requires: is_constructible<first_type, const U&>::value is true and
is_constructible<sec- ond_type, const V&>::value is true.

这里是调用这个构造函数,因为make_pair会returnpair<int, double>,其实还有一个前提:

This constructor shall not participate in overload resolution unless const U& is implicitly convertible to first_type and const V& is implicitly convertible to second_type.

因此,gcc/clang/msvc 是正确的,不应编译此代码,因为 double 不能隐式转换为 A