Konzepte und "das" Abzug "

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Konzepte und "das" Abzug "

by Anonymous » 07 Apr 2025, 04:05

Ich erforsche die Funktion "DIESER" Feature und habe Probleme, zu verstehen, wie ich sie mit Konzepten richtig einsetzen kann. Hier ist, was ich habe: < /p>

Code: Select all

#include 

struct X {
X() = default;
X& operator+=(int x) { value += x; return *this; }
int value = 0;
};

struct Y {
Y() = default;
template Self& operator+=(this Self& self, int x) {
self.value += x; return self;
}
int value = 0;
};

template
concept has_addition_assignment =
std::is_lvalue_reference_v
&& requires(T a, U b) { { a += b } -> std::same_as; };

static_assert(has_addition_assignment); // OK
static_assert(!has_addition_assignment); // OK

static_assert(has_addition_assignment); // OK
// static_assert(!has_addition_assignment); // Fails

int
main()
{
// Not allowed.
//  const Y y;
//  y += 1;
}
< /code>
Das Konzept versucht sicherzustellen, dass x x; x += 1; 
funktioniert, aber const x x; x += 1 nicht. Dies funktioniert für x , aber nicht y trotz consty y; y += 1; fehlschlägt. Was ist der beste Weg zu überprüfen>

Top