c++ - Concept that requires a certain return type of member - Stack Overflow
C++20: Concept详解以及个人理解 - 知乎 (zhihu.com)
C++20详解:Concept - 知乎 (zhihu.com)
一个强迫症友好的C++20 concept的写法 - 知乎 (zhihu.com)
使用concept可以实现描述一个对象的成员属性类型和成员函数类型。
导入部分
export module TestConcept.Class;
import <concepts>;
import <iostream>;
import <string>;
using std::string;
using std::same_as;
using std::common_with;
using std::cout, std::operator&, std::endl;
对于我们的测试类,我们相当于定义了一个name:string、score:int的get set方法。现在我们想要将它抽象为一个接口,用来识别所有满足<拥有public name、score以及其get set方法>的条件的对象。
export class TestClass {
public:
string name;
int score;
auto getName() -> string { return name; }
auto setName(string _name) -> void { name = _name; }
auto getScore() -> int { return score; }
auto setScore(int _score) -> void { score = _score; }
auto moreFun() -> void {}