In C++ how do I pass captured args to a nested lambda that uses std::format? (without getting an error)

37 Views Asked by At

I'm trying to pass a lambda to another function, where the lambda has captured things by value -- not by reference. My error seems specific to std::format, because I was able to get it working with printf.

Here's some minimal sample code (+godbolt) and the error is at the bottom.

I'm using gcc 13.2 with -std=c++23

#include <cstdio>
#include <format>
#include <iostream>

template<typename F>
void do_something_with_lambda(F&& lambda)
{
    // This is not important for the example
}

// ***** This does NOT work *****
template<typename ...Args>
void func(char const* fmt, Args&&... args)
{
    do_something_with_lambda([fmt = fmt, ...args = args]()
        {
            std::cout << std::format(fmt, args...);
        });
}

// ***** This does work *****
template<typename ...Args>
void func2(char const* fmt, Args&&... args)
{
    do_something_with_lambda([fmt = fmt, ...args = args]()
        {
            printf(fmt, args...);
        });
}

int main()
{
    func("hello {}", 12);
    func2("hello %d", 12);
}

The error I get is:

source>: In instantiation of 'void func(const char*, Args&& ...) [with Args = {int}]':
<source>:33:9:   required from here
<source>:17:37: error: '__closure' is not a constant expression
   17 |             std::cout << std::format(fmt, args...);
      |                          ~~~~~~~~~~~^~~~~~~~~~~~~~
Compiler returned: 1

godbolt

I am stumped -- any help would be greatly appreciated. Thanks.

0

There are 0 best solutions below