gpt4 book ai didi

c++ - 即使 V(U) 有效,从 std::pair 到 std::pair 的转换也不起作用?

转载 作者:太空狗 更新时间:2023-10-29 20:56:16 26 4
gpt4 key购买 nike

这是一个适用于 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将返回 pair<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 .

关于c++ - 即使 V(U) 有效,从 std::pair<T, U> 到 std::pair<T, V> 的转换也不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com