The following example fails to compile as soon as the lambda capture any variable. If the capture is removed the compiler will successfully compile the code below.
#include <concepts>
#include <functional>
#include <iostream>
template<typename T>
concept OperatorLike = requires(T t, std::string s) {
{ t[s] } -> std::same_as<std::string>;
};
template<typename T, typename O>
concept Gettable = requires(T t, O op) {
t.apply_post(0, op); };
template<std::semiregular F>
class RestApiImpl {
F m_post_method;
public:
RestApiImpl(F get = F{}) : m_post_method{std::move(get)} {}
template<OperatorLike IF>
requires std::invocable<F, const int, IF>
void apply_post(const int req, IF interface){
m_post_method(req, std::move(interface));
}
};
int main(){
int ii;
auto get = [&ii](int, OperatorLike auto intf){
std::string dummy = "dummy";
std::cout << intf[dummy];
};
RestApiImpl api(get);
return 0;
};
If the lambda function get() has empty brackets [] , meaning no capture it will compile.
The need of the class RestApiImpl is based on following application:
class Server{
public:
struct impl;
void run(Gettable<impl> auto& api){
api.apply_post(0, impl(*this));
}
struct impl {
public:
Server& m_server;
impl(Server& server ) : m_server(server){}
std::string operator[](std::string key) {
return "dummy_for_now";
}
};
};
int main(){
auto get = [&ii](int, OperatorLike auto intf){
std::string dummy = "dummy";
std::cout << intf[dummy];
};
RestApiImpl api(get);
Server server;
server.run(api);
return 0;
};
The error message:
:35:24: error: class template argument deduction failed: 35 | RestApiImpl api(get); | ^ :35:24: error: no matching function for call to 'RestApiImpl(main()::&)' :18:5: note: candidate: 'template RestApiImpl(F)-> RestApiImpl' 18 | RestApiImpl(F get = F{}) : m_post_method{std::move(get)} {} | ^~~~~~~~~~~ :18:5: note: template argument deduction/substitution failed: : In substitution of 'template RestApiImpl(F)-> RestApiImpl [with F = main()::]': :35:24: required from here :18:5: error: template constraint failure for 'template requires semiregular class RestApiImpl' :18:5: note: constraints not satisfied
What options to I have in terms of overcoming this ?`