Clang-format, formatting inconsistent for (seemingly) incorrect user defined literals

Hi all,

the following code compiles well with g++ 12.1.0, but not with clang++ 14.0.6

#include <iostream>
#include <string>

using T = long double;

constexpr T operator"" _€(T amount)
{
    return amount;
}

constexpr T operator"" _$(T amount)
{
    return amount;
}

int main()
{
    auto euros{ 11.02_€ };
    auto dollars{ 12.02_$ };

    std::cout << euros << '\n';
    std::cout << dollars << '\n';

    return 0;
}

The main reason is that clang++ does not accept € and $ in user defined literals.

So far, so good.

The problem then arises with clang-format, which inserts a space between the _ and the $,

    auto euros{ 11.02_€ };
    auto dollars{ 12.02_ $ };

but leaves _€ as well as the lines

constexpr T operator"" _€(T amount)
constexpr T operator"" _$(T amount)

unchanged.

Do I have to set a special option in my .clang-format file?
(I can provide the current file, if needed for the solution)

Hi,
I have the same issue, grrrr…
Did you find a solution/ workaround this?

Just for some extra context, clang-format relies on clang’s own lexer, so it’s unlikely that we (from clang-format) can do much about it without this also being fixed in clang itself

As @rymiel said, it’s a problem in the lexer.

I’ve submitted a patch here that you can check out

1 Like