c++ - pair 的两个构造函数几乎相同,为什么不产生构建错误?
问题描述
在pair的实现中,接下来的两个构造函数只是前缀不同:explicit。这两个成员模板功能几乎相同。
template<class _Other1,
class _Other2,
enable_if_t<conjunction_v<
is_constructible<_Ty1, _Other1>,
is_constructible<_Ty2, _Other2>,
is_convertible<_Other1, _Ty1>,
is_convertible<_Other2, _Ty2>
>, int> = 0>
constexpr pair(_Other1&& _Val1, _Other2&& _Val2)
_NOEXCEPT_COND(is_nothrow_constructible_v<_Ty1, _Other1>
&& is_nothrow_constructible_v<_Ty2, _Other2>)
: first(_STD forward<_Other1>(_Val1)),
second(_STD forward<_Other2>(_Val2))
{ // construct from moved values
}
template<class _Other1,
class _Other2,
enable_if_t<conjunction_v<
is_constructible<_Ty1, _Other1>,
is_constructible<_Ty2, _Other2>,
negation<conjunction<
is_convertible<_Other1, _Ty1>,
is_convertible<_Other2, _Ty2>>>
>, int> = 0>
constexpr explicit pair(_Other1&& _Val1, _Other2&& _Val2)
_NOEXCEPT_COND(is_nothrow_constructible_v<_Ty1, _Other1>
&& is_nothrow_constructible_v<_Ty2, _Other2>)
: first(_STD forward<_Other1>(_Val1)),
second(_STD forward<_Other2>(_Val2))
{ // construct from moved values
}
但是当我如下编写测试示例时:
class A
{
public:
template<typename T1,typename T2>
A(T1 a,T2 b){}
template<typename T1,typename T2>
explicit A(T1 a,T2 b){}
};
生成错误如下:
Error C2535 'A::A(T1,T2)': member function already defined
那么为什么结果不同呢?
解决方案
下面的类不使用上面的模板。
该错误是由于您两次描述相同的构造函数而引起的。
explicit
只是如何调用构造函数的限制,而不是允许重载的签名更改。
推荐阅读
- java - Spring Jpa 继承类型加入 select only 基类
- amazon-web-services - 我们如何在 AWS(EC2 实例)中管理 Web 服务器和应用程序服务器?
- database - 哪种架构用于多个小型特定服务器和集中式服务器?
- firebase - firestore 是否支持事务中的集合级别锁定?
- java - CTCI 制作字谜 - 得到不正确的输出
- python-3.x - 如何每次在我的列表之一中删除 '\n' [Python]
- react-native - undefined 不是对象(评估 'RNGestureHandlerModule.State'
- string - 嗨,当我尝试从文件中拆分字符串时,当我将其保存在 List Python 中时,它需要一个空格作为元素
- sql - SQL Where 子句 - 遇到时创建字段
- spring - Spring Cloud Vault 不适用于自定义挂载