Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

鸭子类型与 C++20 concept 中 自带干粮 部分的疑问 #79

Open
muyuuuu opened this issue Jan 3, 2025 · 2 comments
Open

鸭子类型与 C++20 concept 中 自带干粮 部分的疑问 #79

muyuuuu opened this issue Jan 3, 2025 · 2 comments

Comments

@muyuuuu
Copy link
Contributor

muyuuuu commented Jan 3, 2025

#include <concepts>
#include <iostream>
#include <type_traits>

class Data {
public:
    Data(int x, int y) : x{x}, y{y} {}
    int x;
    int y;
    void show() {
        std::cerr << x << " " << y << "\n";
    }
};

不行:

const bool a = requires (Data d{1, 2}) {d.show();};

行:

const bool a = requires (Data d = Data{1, 2}) {d.show();};

是为什么

@frederick-vs-ja
Copy link
Contributor

frederick-vs-ja commented Jan 6, 2025

此处语法与函数的默认实参一致,必须有 =

@frederick-vs-ja
Copy link
Contributor

不对。这是错误的, requires 表达式的参数列表不能有默认实参。

目前 GCC 接受这种错误的写法(链接),此为已经确认的 bug。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants