<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Topics tagged with c++23]]></title><description><![CDATA[A list of topics that have been tagged with c++23]]></description><link>https://community.secnto.com//tags/c++23</link><generator>RSS for Node</generator><lastBuildDate>Mon, 08 Jun 2026 20:45:39 GMT</lastBuildDate><atom:link href="https://community.secnto.com//tags/c++23.rss" rel="self" type="application/rss+xml"/><pubDate>Invalid Date</pubDate><ttl>60</ttl><item><title><![CDATA[std::print() wrapper with altered format]]></title><description><![CDATA[@zaasmi said in std::print() wrapper with altered format:

Here’s a rewritten version of your question:
“I’m trying to create a std::print() wrapper that modifies the output format, such as adding a prefix and suffix around the original format. I was able to achieve this using a macro. How can I improve this approach or implement it differently for more flexibility?”
#define DECORATED_PRINT(fmt, ...) std::println("prefix " fmt " suffix", __VA_ARGS__)



To achieve a std::print() wrapper with a modified format (e.g., adding a prefix and suffix), using a macro is one way, but there are other approaches that might offer more flexibility and better maintainability. Here are a few options:

Using a Macro

You can use a macro to wrap std::print() and modify the format. Here’s a simple example:
#define PRINT_WITH_FORMAT(fmt, ...) std::print("[Prefix] " fmt " [Suffix]\n", ##__VA_ARGS__)

While this works, macros have limitations in terms of type safety, debugging, and maintainability, especially for larger projects.

Using a Function Template

For more flexibility, you can create a function template that wraps std::print() and modifies the format. This way, you avoid the pitfalls of macros while still achieving the same goal:
#include &lt;format&gt;
#include &lt;iostream&gt;

template&lt;typename... Args&gt;
void print_with_format(const std::string&amp; fmt, Args&amp;&amp;... args) {
    std::string formatted_message = std::format("[Prefix] " + fmt + " [Suffix]\n", std::forward&lt;Args&gt;(args)...);
    std::cout &lt;&lt; formatted_message;
}

Usage:
print_with_format("Hello, {}!", "World");


Custom Formatter (Advanced)

If you want more control over the formatting process, you can create a custom formatter by extending std::formatter:
#include &lt;format&gt;
#include &lt;iostream&gt;

template &lt;&gt;
struct std::formatter&lt;std::string&gt; {
    constexpr auto parse(auto&amp; ctx) {
        return ctx.begin();
    }

    auto format(const std::string&amp; s, auto&amp; ctx) {
        return std::format_to(ctx.out(), "[Prefix] {} [Suffix]", s);
    }
};

void print_with_format(const std::string&amp; message) {
    std::cout &lt;&lt; std::format("{}", message) &lt;&lt; '\n';
}

This approach gives you deep customization over how different types are formatted.
Conclusion
•	Macros are quick and simple but have limitations in flexibility and maintainability.
•	Function templates offer a more modern, type-safe, and maintainable solution.
•	Custom formatters provide advanced control and customization, especially for complex formatting needs.
For most use cases, the function template approach strikes a good balance between flexibility and simplicity.
]]></description><link>https://community.secnto.com//topic/2679/std-print-wrapper-with-altered-format</link><guid isPermaLink="true">https://community.secnto.com//topic/2679/std-print-wrapper-with-altered-format</guid><dc:creator><![CDATA[zaasmi]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>