Question Details

No question body available.

Tags

c++ constexpr consteval std-source-location

Answers (1)

Accepted Answer Available
Accepted Answer
August 5, 2025 Score: 6 Rep: 229,739 Quality: Expert Completeness: 80%

Function parameters are not constant expression, even in consteval function.

You have to pass argument as template.

Since C++20, you might have structural types as template parameter, so

constexpr std::sizet constexprstrlen(const char s)
{
    std::size_t i = 0;
    while (s[i]) ++i;
    return i;
}

template // Some hard coded limit for non literal string struct LiteralString { consteval LiteralString(const char
s) { std::copy(s, s + constexprstrlen(s), &data[0]); } consteval LiteralString(const char (&s)[N]) { std::copy(s, s + N, &data[0]); }

static constexpr std::size
t size = N; std::array data{}; };

Then you might do something like:

template 
void foo() {
    constexpr std::sizet len = constexprstrlen(S.data.data());
    // ...
}

int main() { foo(); // empty string for clang/msvc :( foo(); // empty string for clang/msvc :( }

Unfortunately, only gcc feeds current sourcelocation in template parameter Demo.

MACRO might be a workaround Demo

#define FOO() foo()

int main() { foo(); // empty string for clang/msvc :( foo(); // empty string for clang/msvc :(

// "int main()" or "int
_cdecl main(void)" foo(); FOO(); }