Skip to content

Clang 19.1.0 - Extremely poor error message when dealing with callback functions and std::format #127236

@9291Sam

Description

@9291Sam

Here's an MRE for this error.

https://godbolt.org/z/b4sErhzqb

#include <iostream>
#include <vector>
#include <concepts>

struct CallBack
{
    void callback(std::invocable<int, std::size_t> auto func)
    {
        for (std::size_t i = 0; i < foo.size(); ++i)
        {
            func(foo[i], i);
        }
    }

    std::vector<int> foo;
};

void caller()
{
    CallBack c {};

    c.callback(
        [&](int i, std::size_t s)
        {
            // the error is below, there are 4 {} which correspond with only 3 arguments
            std::cout << std::format("{} {} {} {}", s, i, false);
        });
}

->

<source>:11:13: error: call to immediate function 'caller()::(anonymous class)::operator()' is not a constant expression
   11 |             func(foo[i], i);
      |             ^
<source>:22:7: note: in instantiation of function template specialization 'CallBack::callback<(lambda at <source>:23:9)>' requested here
   22 |     c.callback(
      |       ^
<source>:11:18: note: implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function
   11 |             func(foo[i], i);
      |                  ^
1 error generated.
Compiler returned: 1

Metadata

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconstexprAnything related to constant evaluationformatC++20 std::format or std::print, and anything related to them

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions