CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,755

    Question Proper way to print - think C++ 23+

    I'm curious -

    What is the proper way to print in a modern C++ program. Don't think about how you've aways printed with the simply using cout like this:
    Code:
    #include <iostream>
    
    int main() {
        std::string message = "The number is";
        int number = 42;
    
    
        std::cout << message << ": " << number << std::endl;
        return 0;
    }
    Rather think C++ 23. Where things like format have been added:

    Code:
    #include <iostream>#include <format>
    
    int main() {
        std::string message = "The number is";
        int number = 42;
    
        std::cout << std::format("{}: {}\n", message, number);
        return 0; }

    Or would you now do it using std::print?

    Code:
    #include <print> 
    int main() {
        int number = 42;
        std::string message = "The number is";
    
        std::print("{}: {}\n", message, number);
        return 0; }
    Or is std::print not yet fully accepted, and thus the best way is to use format?

    Again, I don't care as much about what people are doing, but rather about what people SHOULD Be doing if they are following current, modern standards.

    Thoughts?
    Last edited by Brad Jones; March 30th, 2025 at 11:20 AM.
    -----------------------------------------------
    Brad! Jones,
    Yowza Publishing
    LotsOfSoftware, LLC

    -----------------------------------------------

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Proper way to print - think C++ 23+

    Should is std:: print - if available. If not then std::format_to
    std::format_to(std:: ostream_iterator<char>(std::cout), "{}: {}\n", message, number);

    This removes the need for a temp std::string.
    Note that the iterator can be set once:

    Code:
    const auto fmt_to { std:: ostream_iterator<char>(std::cout) };
    
    std::format_to(fmt_to, "{}: {}\n", message, number);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured