Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Last active August 27, 2024 20:59
Show Gist options
  • Save matu3ba/b85d0585e3e509b194a6257687707479 to your computer and use it in GitHub Desktop.
Save matu3ba/b85d0585e3e509b194a6257687707479 to your computer and use it in GitHub Desktop.
concepts_check_multiply_operator_example
#include<iostream>
#include<type_traits>
#include<cstdio>
#include<concepts> // c++20
struct A {
bool operator == (A const &);
int operator * (int factor) {
return m * factor;
}
int m;
};
template<typename T1, typename T2>
concept CanMultiply = requires(T1 & a, T2 & b) {
a * b;
};
template<typename T1, typename T2> requires CanMultiply<T1, T2>
void mul(T1 & t1, T2 & t2) {
t1.m = t1 * t2;
}
int main() {
A a = { 2 };
int b = 10;
mul( a, b );
fprintf(stdout, "a: %d\n", a.m);
return 0;
}
// usage: clang++ -std=c++20 -Weverything -O3 .\concepts_check_mul_op.cpp && .\a.exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment