简体   繁体   English

C++ 两个模板,相同的名称,相同的签名,不同的类:如何强制编译器使用预期的一个?

[英]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.

相关问题 编译器如何区分C ++中不同类中具有相同名称的静态数据成员? - How can a compiler differentiate between static data members having same name in different classes in C++? 继承-在两个不同的类中使用相同的方法名称 - c++ - inheritance- the same method name in two different classes C ++:继承两个具有相同名称,不同名称空间的基类 - C++: Inheriting two base classes with same name, different namespace 具有相同名称但签名不同的多个构造函数定义(C ++) - Multiple constructors definitions with same name but different signatures (C++) 如何隔离两个同名的c ++类? - How to isolate two c++ classes with the same name? C++ 在数组中存储具有不同模板的相同类 - C++ store same classes with different templates in array C ++:强制编译器使用两个竞争运算符之一 - C++: force the compiler to use one of two competing operators C ++:不同翻译单元中具有相同名称的不同类 - C++: Different classes with the same name in different translation units 如何使用CMake强制c ++编译器使用不同安装包的版本之一? - How to force c++ compiler use one of different installed package's versions, using CMake? 如何在C ++中的两个不同对象中使用相同的变量名 - How to use the same variable names in two different Objects in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM