C++ Logo

std-discussion

Advanced search

Lambda inside concept

From: Federico Kircheis <federico_at_[hidden]>
Date: Fri, 12 Jan 2024 22:24:45 +0100
Hello to everyone,

I'm trying to write a concept, but I'm not sure if what I'm writing is
valid C++, or if it is possible to express at all:

given following concept and functions

template <typename T>
concept destructible = requires (T a) {
   [](T a){auto [b] = a;}(a);
};


void fun(destruct1 auto y) {
}
template <class T>
auto fun(T y) {
}

Following code compiles on gcc, clang and msvc

----
struct s1 {
   int v;
};
fun(s1{});
----
but
----
struct s2 {
   int v;
   int v2;
};
fun(s2{});
----
compiles only with msvc.
both clang and gcc diagnose that s2 cannot be decomposed in two elements.
Thus my first question is, is how I am using the lambda incorrect?
I've noticed that I get a similar error when using
template <typename T>
concept Addable = requires (T a, T b) {
   [](T a, T b){a + b;}(a,b);
};
instead of
template <typename T>
concept Addable = requires (T a, T b) {
   a + b;
};
The first concept works in msvc, but not clang and gcc.
Supposing that using a lambda is not correct, here comes my second question:
Is it possible to express as a concept that a structure can be 
decomposed in one (or N) elements?
auto [b] = a;
is not an expression...
Federico

Received on 2024-01-12 21:24:51