【问题标题】:Is the following case an issue with the new C++ concept standard? [duplicate]以下情况是否与新的 C++ 概念标准有关? [复制]
【发布时间】:2021-12-29 18:47:38
【问题描述】:

考虑这个使用curiously recurring template pattern的程序:

template <typename T>
concept Frobulates = requires (T t) { t.frobulate(); };

template <typename Derived>
struct S {
    int sidefumble() requires Frobulates<Derived> {
        Derived::frobulate();
    }
};

struct K: public S<K> {
    void frobulate() {}
};

int main() {
    K k;
    k.sidefumble();
}

编译时,使用clang++ 13.0.0 版生成此错误:

$ clang13 -std=c++20 ./recurring-concept.cpp -o recurring-concept && ./recurring-concept
./recurring-concept.cpp:17:7: error: invalid reference to function 'sidefumble': constraints not satisfied
    k.sidefumble();
      ^
./recurring-concept.cpp:6:31: note: because 'K' does not satisfy 'Frobulates'
    int sidefumble() requires Frobulates<Derived> {
                              ^
./recurring-concept.cpp:2:41: note: because 't.frobulate()' would be invalid: no member named 'frobulate' in 'K'
concept Frobulates = requires (T t) { t.frobulate(); };
                                        ^
1 error generated.

具体来说,编译器报告K 类型没有成员frobulate(),但这显然是错误的。

我希望sidefumble() 是可编译的,就像requires 语句不存在时一样。

  • 这是预期/设计的行为吗? (在我看来,可能使用 CRTP 是一种不寻常的极端情况,在这个非常年轻的功能的设计和/或实现中可能没有考虑到)
  • 至少,该错误消息具有严重的误导性。编译器/标准作者之间就该案例的处理展开讨论的合适地点在哪里?
  • 有解决方法吗?

【问题讨论】:

  • @T.C.,这似乎是相关的,但我不确定它是否直接回答了这个案例。不过,这很有帮助!
  • @trbabb TC 说您遇到了 clang 错误。

标签: c++ language-lawyer clang++ crtp c++-concepts


【解决方案1】:

看起来这是一个错误。

虽然this other question(感谢@T.C.)似乎概述了稍微不同的场景,但潜在的错误似乎是相同的。

This comment in a clang bug thread 包含几乎完全相同的复制品。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多