检查我是否可以将 class 分配给另一个的最通用方法

Most general way to check if I can assign a class to another

在 C++ 中,给定两个 类、ab,检查某些表达式是否为

的最通用方法是什么
my_a = my_b;

会有意义吗?我考虑过使用 is_convertible 但这没有检测到强制转换运算符。也许它只适用于原始类型?我希望我的测试产生 true 如果:

等等。有什么办法可以完成这样的事情吗?

std::is_assignable:

bool x = std::is_assignable<decltype(my_a), decltype(my_b)>::value;

您可以检查 is_constructible,这包括转换。