cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] Better "printf debugging"

Open DyXel opened this issue 6 months ago • 4 comments

Essentially, implement the "self-documenting f-strings" from Python (link to feature). Note that today most of Python-style formatting is already supported OOTB (Link to cppfront documentation), since C++'s standard format specification is based on Python's specification (cppreference), this is just a small but nice productivity enhancement to have.

Examples:

>>> my_var = 42
>>> f"Prefix|{my_var}|Postfix"
'Prefix|42|Postfix'
>>> f"Prefix|{my_var=}|Postfix"
'Prefix|my_var=42|Postfix'
>>> f"Prefix|{my_var =}|Postfix"
'Prefix|my_var =42|Postfix'
>>> f"Prefix|{my_var = }|Postfix"
'Prefix|my_var = 42|Postfix'
>>> my_f = lambda x: x+2
>>> f"Prefix|{my_f(2)}|Postfix"
'Prefix|4|Postfix'
>>> f"Prefix|{my_f(2)=}|Postfix"
'Prefix|my_f(2)=4|Postfix'
>>> f"Prefix|{my_f(2) =}|Postfix"
'Prefix|my_f(2) =4|Postfix'
>>> f"Prefix|{my_f(2) = }|Postfix"
'Prefix|my_f(2) = 4|Postfix'

You can then imagine how cppfront's would look like:

test: std::string_view = "My String";
main: () = std::cout << "Prefix|(test = )$|Postfix";

// Outputs: Prefix|test = My String|Postfix

DyXel avatar Jun 30 '25 17:06 DyXel

Oh I wonder if a user of the language could implement that based on the tools that the language (wants to eventually) offer. If all the reflection functionality were already working, could I apply it somehow to a user-defined literal and read the code written within the curly braces?

jcanizales avatar Jun 30 '25 20:06 jcanizales

If the entirety of the language was reflectable, then yeah, I don't see why that wouldn't be possible; You write a metafunction/metaprogram that checks for usage of string interpolation and mutates it so that a custom implementation is injected. For example, to use a custom allocator / string builder object.

DyXel avatar Jun 30 '25 21:06 DyXel

Oh I wonder if a user of the language could implement that based on the tools that the language (wants to eventually) offer. If all the reflection functionality were already working, could I apply it somehow to a user-defined literal and read the code written within the curly braces?

Btw, this could (should?) probably be a discussion thread, but I read both p2996 "Reflection For C++26" (Voted in for next standard!) and p3294 "Code Injection with Token Sequences" (Not voted in unfortunately), and I got to say, I am not sure what a reflection of actual code would look like, or if its even possible to write one, but to me it seems like the Profiles proposals would allow you to analyze code and reject certain "unsafe" constructs? it would be a cool discussion to have on ideas on how can this be realized in the future for C++.

DyXel avatar Jul 07 '25 11:07 DyXel

Yeah my current understanding is that C++2's current reflection roadmap doesn't reach the point of supporting this as user coed.

jcanizales avatar Jul 08 '25 17:07 jcanizales