std::format Extension

Displaying the address of an arbitrary pointer in C++ 20 fails but succeeds with C++26.

C++20

Only void, const void, and std::nullptr_t pointer types are valid. If you want to display the address of an arbitrary pointer, you must cast it to (const) void*.

// formatPointer20.cpp

#include <format>
#include <iostream>
#include <string>

int main() {

  double d = 123.456789;

  //std::cout << "&d" << std::format("{}", &d) << '\n';  
  std::cout << "static_cast<void *>(&d): " << std::format("{}", static_cast<void *>(&d)) << '\n';     
  std::cout << "static_cast<const void *>(&d): " << std::format("{}", static_cast<const void *>(&d)) << '\n'; 
  std::cout << "nullptr: " << std::format("{}", nullptr) << '\n';           
             
}

In the main function, a double variable named d is initialized with the value 123.456789. The program then prints the address of this variable using std::format.

The first commented-out line attempts to print the address of d directly using std::format("{}", &d). This line is commented out because it would result in a compilation error. The std::format function requires the pointer to be cast to void* or const void* to format it correctly.

The next three lines demonstrate the correct way to format and print pointers using std::format. The first of these lines prints the address of d after casting it to void*. The second line prints the address of d after casting it to const void*. The third line prints the value nullptr, which represents a null pointer.

Each of these lines uses std::cout to print a descriptive message followed by the formatted pointer. The std::format function is used to convert the pointer to a string representation that can be printed to the console.

Here is the output of the program:

Enabling the commented-out line causes a long error message:

C++26

With C++26, you can directly output the pointer:

// formatPointer26.cpp

#include <format>
#include <iostream>
#include <string>

int main() {

  double d = 123.456789;
  double* p = &d;

  std::cout << "&d" << std::format("{:P}", p) << '\n';  
 
}

Honesty, the program does not compile. This contradicts compiler support for C++26 and the proposal P2510R3. Do you know why?

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (open)
  • "Embedded Programming with Modern C++": January 2025
  • "Generic Programming (Templates) with C++": February 2025
  • "Clean Code: Best Practices for Modern C++": May 2025
  • Do you want to stay informed: Subscribe.

     

    A solution will be provided in the next article.

    What’s Next?

    Concurrency will get with C++26 two nice features for lock-free data structures: Read-copy update and hazard pointers.

    Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, and Honey Sukesan.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Modernes C++ GmbH

    Modernes C++ Mentoring (English)

    Do you want to stay informed about my mentoring programs? Subscribe Here

    Rainer Grimm
    Yalovastraße 20
    72108 Rottenburg

    Mobil: +49 176 5506 5086
    Mail: schulung@ModernesCpp.de
    Mentoring: www.ModernesCpp.org

    Modernes C++ Mentoring,