#include <kari.hpp>
#include <cstdlib>
template<class C>
struct curry_printf_wrapper {
C c;
bool applicated;
curry_printf_wrapper(const C& c)
: c(c)
, applicated(false) {
}
~curry_printf_wrapper() {
if (!applicated) {
c();
}
}
decltype(auto) operator()(auto arg) -> curry_printf_wrapper<decltype(c(arg))> {
applicated = true;
return curry_printf_wrapper<decltype(c(arg))>(c(arg));
}
};
auto curry_printf(const char* format_str) {
auto c = kari::curryV([format_str](auto&&... args) {
printf(format_str, std::forward<decltype(args)>(args)...);
});
return curry_printf_wrapper<decltype(c)>(c);
}
template<class C>
struct curry_sprintf_wrapper {
C c;
curry_sprintf_wrapper(const C& c)
: c(c) {
}
std::string operator()() {
return c();
}
decltype(auto) operator()(auto arg) -> curry_sprintf_wrapper<decltype(c(arg))> {
return curry_sprintf_wrapper<decltype(c(arg))>(c(arg));
}
operator std::string() {
return c();
}
};
auto curry_sprintf(const char* format_str) {
auto c = kari::curryV([format_str](auto&&... args) {
std::string str;
str.resize(100);
sprintf(&str[0], format_str, std::forward<decltype(args)>(args)...);
return str.c_str();
});
return curry_sprintf_wrapper<decltype(c)>(c);
}
int main(int argc, char* argv[]) {
curry_printf("%d %d")(1)(2);
std::string s1 = curry_sprintf("%d %d")(1)(2)();
std::string s2 = curry_sprintf("%d %d")(1)(2);
return 0;
}
tst.cpp: In instantiation of 'auto:3 curry_sprintf_wrapper<C>::operator()(auto:3) [with auto:3 = int; C = kari::curry_t<18446744073709551615ul, curry_sprintf(const char*)::<lambda(auto:4&& ...)> >]':
/home/sl/project/Elephantdrive/devroot/develop/src/test/case/tst.cpp:108:43: required from here
/home/sl/project/Elephantdrive/devroot/develop/src/test/case/tst.cpp:88:56: error: cannot convert 'curry_sprintf_wrapper<kari::curry_t<18446744073709551614ul, curry_sprintf(const char*)::<lambda(auto:4&& ...)>, int> >' to 'int' in return
return curry_sprintf_wrapper<decltype(c(arg))>(c(arg));