Konzepte und "das" Abzug "C++

Programme in C++. Entwicklerforum
Anonymous
 Konzepte und "das" Abzug "

Post by Anonymous »

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>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post