即使V(U)有效,从std :: pair <t,u =“”>到std :: pair <t,v =“”>的转换也不起作用?

时间:2015-11-30 08:57:16

标签: c++ c++11 std-pair

这是一个适用于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上它并没有提到类似的东西。

1 个答案:

答案 0 :(得分:4)

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将返回pair<int, double>,实际上还有另一个前提条件:

  

此构造函数不应参与重载决策,除非   const U&amp;可隐式转换为first_type和const V&amp;是   隐式转换为second_type。

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

相关问题