Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_convertible模板实例化直接用is_convertible<U1, Ty1>不可以吗 #162

Open
Peas-Li opened this issue Oct 28, 2024 · 2 comments
Open

Comments

@Peas-Li
Copy link

Peas-Li commented Oct 28, 2024

template <class U1 = Ty1, class U2 = Ty2,
typename std::enable_if<
std::is_copy_constructible::value &&
std::is_copy_constructible::value &&
std::is_convertible<const U1&, Ty1>::value &&
std::is_convertible<const U2&, Ty2>::value, int>::type = 0>
constexpr pair(const Ty1& a, const Ty2& b)
: first(a), second(b)
{
}

// explicit constructible for this type
template <class U1 = Ty1, class U2 = Ty2,
typename std::enable_if<
std::is_copy_constructible::value &&
std::is_copy_constructible::value &&
(!std::is_convertible<const U1&, Ty1>::value ||
!std::is_convertible<const U2&, Ty2>::value), int>::type = 0>
explicit constexpr pair(const Ty1& a, const Ty2& b)
: first(a), second(b)
{
}

@frederick-vs-ja
Copy link
Contributor

is_convertible<const U1&, Ty1>is_convertible<U1, Ty1> 含义确实不一样。

@Peas-Li
Copy link
Author

Peas-Li commented Oct 30, 2024

其实,只有pair的两个参数被实例化为引用类型即,pair<int&, double&>,第一个构造函数才会被调用,而此时U和T都为引用类型,在std::is_convertible<const U1&, Ty1>::value中的const U1&,const只是顶层const,给引用类型加顶层const并没用,所以std::is_convertible<const U1&, Ty1>::value = 1,也就是说此时同样的std::is_convertible<U1, Ty1>::value = 1,所以
虽然is_convertible<const U1&, Ty1> 与 is_convertible<U1, Ty1> 含义确实不一样,但是在这里编译器认为它们的含义一样

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants