Wie kann ich überprüfen, ob ein Funktionsergebnis Constexpr ist, auch wenn das Argument nicht ist?C++

Programme in C++. Entwicklerforum
Anonymous
 Wie kann ich überprüfen, ob ein Funktionsergebnis Constexpr ist, auch wenn das Argument nicht ist?

Post by Anonymous »

Code: Select all

constexpr
Funktionen können KonsequPRPR Ergebnisse liefern, auch wenn ihre Argumente nicht konstant sind, wenn sie nicht genutzt werden. Dies ist zum Beispiel std :: integral_constant :: operator t :

Code: Select all

#include 

std::integral_constant a;
constexpr int b = a; // Compiles!
Wie kann ich ein Konzept erstellen, um diese Eigenschaft einer Funktion zu testen? rennen auf gcc.godbolt.org

Code: Select all

#include 

struct A {explicit constexpr operator bool() const {return true;}};
struct B {int x = 1; explicit constexpr operator bool() const {return x;}};

template 
concept C = requires(T t){std::bool_constant{};};

static_assert(C);
static_assert(!C);
< /code>
Aber das gibt mir: < /p>
:7:46: error: constraint variable 't' cannot be used in an evaluated context
7 | concept C = requires(T t){std::bool_constant};
|                                              ^

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post