小编Oun*_*ces的帖子

我在 C++ 中使用 void swap() 对这个函数进行模板化时有什么巧合吗?

我目前使用的是 C++ 17,我想简化我的程序。但是,我被抛出错误:

more than one instance of overloaded function \"swap\" matches the argument list: -- function template \"void swap(T &a, T &b)\" -- function template \"std::enable_if<std::__and_<std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp>>::value, void>::type std::swap(_Tp &__a, _Tp &__b)\" -- argument types are: (int, int)",
Run Code Online (Sandbox Code Playgroud)

这是我写的代码:

#include <iostream>
#include <string>
using namesapce std;
template <typename T>
void swap(T &a, T &b) {
    T temp = a;    //Temporary copy to reuse
    a = b;
    b = temp;
}
int main() {
    int x, y;
    cin >> x …
Run Code Online (Sandbox Code Playgroud)

c++ templates function

1
推荐指数
1
解决办法
57
查看次数

标签 统计

c++ ×1

function ×1

templates ×1