This works:

 

#include<iostream>

#define STRINGIFY(s) STRINGIFY2(s)
#define STRINGIFY2(s) #s

int main()
{
    std::cout << R"(pre )" STRINGIFY(__LINE__) R"( post)" << std::endl;
    std::cout << R"(pre )" STRINGIFY(__LINE__) " post" << std::endl;
    std::cout << "pre " STRINGIFY(__LINE__) " post" << std::endl;
    return0;
}

Prints: "pre 8 post" - 3x with different line numbers 8, 9, 10

 

https://godbolt.org/z/a5bceoexG

 

So option A should work similar with F and X strings?

 

How do F strings react, when they are concatenated out of different types of strings? Does each F one of them have to be complete? Or can a F be mixed with a normal string and the concatenation has to be complete?

 

 

If instead of STRINGIFY(__LINE__) we were to put C++ code inside the STRINGIFY macro inside a {}, then the preprocessor would replace symbols and the C++ compiler would execute the code as it is inside {} of a F or X string?

 

So for convenience we could define a macro __X(expression, format) that does all that: STRINGIFY, Create {}, and concatenate without changing the type?

 

// a+b printed as hexadecimal:

 

std::print(X"the sum of a and b is " __X(a+b, "x") ".");

 

Or such a macro is called from within the quotations: Option B

std::print(X"the sum of a and b is #a+b, x#.");

 

Or such a macro is implicitly run by defining the F string rules for the preprocessor phases: Option E

std::print(X"the sum of a and b is {(a+b):x}.");
 

-----Ursprüngliche Nachricht-----
Von: Hadriel Kaplan <hkaplan@juniper.net>
Gesendet: So 15.10.2023 18:45
Betreff: Re: [std-proposals] Supporting f-strings in C++: draft-R1
An: std-proposals@lists.isocpp.org;
CC: Sebastian Wittmeier <wittmeier@projectalpha.org>;
> From: Std-Proposals <std-proposals-bounces@lists.isocpp.org> on behalf of Sebastian Wittmeier via Std-Proposals <std-proposals@lists.isocpp.org>

> you write that there is no preprocessor support within the f-strings.
> Is it possible to concatenate preprocessor macro results with preprocessor string concatenation?
> If yes, would it be further possible to have the preprocessor preprocess code in its usual way, put it through a STRINGIFY macro and concatenate it into the f string?

You can use preprocessor macros to stringify other macro expansions as you can do today, of course - but as far as I know the preprocessor cannot concatenate two string literals into one token literal using the `##`. It's usually the phase-6 translation right afterwards that concatenates them instead.

So then I don't see how you'd get it to become an f-string, since presumably you'd want to join multiple stringified literals into one final f-string.

For just _one_ stringified macro I think you can do it, but that doesn't seem useful.

I.e., you might be able to get a stringified `__LINE__` for line 42 to become a `F"42"`, but if you wanted it to become `F"{42:x}"` instead, I don't think that's possible. (though I'm no PP expert)

I mean basically think of the `F"..."` or `X"..."` as being similar to `R"(...)"` in terms of tokens, and you can try to create an `R"(...)"` using the preprocessor today to see if it's possible - I don't think it is, but I might be wrong. If it _is_ possible then it should be possible to do for f-strings too.

---

Or maybe I misunderstand you - are you instead saying: "I know macro-magic won't work with f-strings as defined, but can the proposal define some way for it to work?"

-hadriel



Juniper Public