【发布时间】:2020-09-28 16:01:06
【问题描述】:
我正在尝试 C++20 的概念,std::swappable_with 未定义(Visual Studio,使用 /std:c++latest)或其约束与下面的 MCVE 不匹配(g++10 使用 -std=c++2a) -- 也就是说,int 不能与int (!) 交换。有什么办法解决这个问题?如果int 不能与int 交换,我看不到任何东西 工作。
#include <concepts>
template <typename T, typename U>
requires std::swappable_with<T,U>
void mySwap(T& t, U& u)
{
T temp = t; t = u; u = temp;
}
int main()
{
int x, y;
mySwap(x, y);
return 0;
}
【问题讨论】:
-
std::is_swappable_with_v<int, int>给我 0 与 libstdc++:godbolt.org/z/n4U-W6 -
我没有 C++20,但你可以检查一下:std::is_copy_assignable
::value 两种类型,然后交换。如果没有什么对你有用。
标签: c++ c++20 c++-concepts