[英]C++ Two templates, same name, same signatures, different classes: How to force compiler to use intended one?
I am trying to use the following C++ template:我正在尝试使用以下 C++ 模板:
_CONSTEXPR20 void swap(_Ty& _Left, _Ty& _Right) noexcept(
is_nothrow_move_constructible_v<_Ty>&& is_nothrow_move_assignable_v<_Ty>) {
_Ty _Tmp = _STD move(_Left);
_Left = _STD move(_Right);
_Right = _STD move(_Tmp);
}
from the Microsoft utility standard header (core), but the compiler keeps using the following template:来自 Microsoft 实用程序标准头文件(核心),但编译器一直使用以下模板:
inline void swap(thread& _Left, thread& _Right) noexcept {
_Left.swap(_Right);
}
from the Microsoft thread standard header.来自 Microsoft 线程标准标头。
How to force the compiler to use the first implementation?如何强制编译器使用第一个实现?
As mentioned in the comments, the compiler will prefer a non-template overload of a function over one that is a template.正如评论中提到的,编译器更喜欢函数的非模板重载而不是模板。
If you need to use the template version, just pass the type argument explicitly:如果您需要使用模板版本,只需显式传递类型参数:
swap<std::thread>(thread1, thread2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.