std :: iS_ASSIGN和抽象类

发布于 2025-01-22 20:02:13 字数 594 浏览 3 评论 0原文

假设您有一个抽象类:

struct Abstr { virtual void f() = 0; };
static_assert(std::is_assignable<Abstr, Abstr>::value);

上面的断言失败了GCC(在版本10.3和11.2上进行了测试),但在Clang上编译恰好(在版本12和13上进行了测试)。

同样,通过使用概念的自定义实现特征:

template <class T, class U>
concept is_assignable_concept = requires
{ std::declval<T>() = std::declval<U>(); };
static_assert(is_assignable_concept<Abstr, Abstr>);

任何编译器的断言永远不会失败。

这里的正确行为是什么? 两者都是受人尊敬的编译器,但我倾向于相信Clang就在这里。

PD: 我想知道为什么不使用概念来实现特质。至少在这种情况下,事实证明它比Sfinae版本要短得多,而且行为显然正确。

Suppose you have an abstract class:

struct Abstr { virtual void f() = 0; };
static_assert(std::is_assignable<Abstr, Abstr>::value);

The assertion above fails with GCC (tested on version 10.3 and 11.2), but compiles just fine on Clang (tested on versions 12 and 13).

Also, with a custom implementation of the trait using concepts:

template <class T, class U>
concept is_assignable_concept = requires
{ std::declval<T>() = std::declval<U>(); };
static_assert(is_assignable_concept<Abstr, Abstr>);

the assertion never fails with either compiler.

What is the correct behavior here?
Both are respected compilers, but I'm inclined to believe Clang is right here.

PD:
I wonder why not use concepts to implement traits. At least in this case, it turns out to be much shorter than the sfinae version and with apparently correct behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文