A student recently brought me a simple introductory-level problem found on the web and asked if their program is “efficient”. Though the problem is simple enough that most C++ beginners are likely to solve it in minutes, I noticed it provides good opportunity to discuss certain efficiency considerations that are not typically discussed in an introductory course.
This post describes the problem and invites students to develop alternative solutions and analyze the runtime space and time (memory and speed) needs of the solutions. I encourage students who have completed a course in C++ to ponder this problem, especially if they have completed multiple C++ courses. The overall goal is to become aware of implementation choices and their consequences.More >>
This post describes some macros I routinely use when experimenting with code. It provides a step-by-step exposition of the use cases and the design leading to the use of variadic macros to satisfy requirements. In the process, the post also touches on the decision (and a need) to use macros instead of function templates.
There is decidedly not much to the macros, but I chose to describe them because there is much educational value due to some tricky issues that need to be addressed in a practical solution.
Update July 30, 2020: I added a section about printing the text of an expression containing commas. I also added four exercises related to the new section.More >>
This post discusses the effect of
const qualifications in pointer declarations,
specifically the distinction between pointer
constness and data
constness. It first
const qualifications of pointers to non-array data, and then examines
qualifications of pointers to array data, including arrays of pointers. In all cases, the
post discusses declarations of variables, function parameters, and function return types.
It assumes the reader is familiar with pointers and the relationship between arrays and
This post is motivated by the observation that those new to pointers tend to mistake
constness in a pointer declaration with pointer
constness. It is also motivated
by the need to emphasize the subtleties of parameter declarations in functions that
receive arrays of pointers (such as command-line arguments).
But first, some advice: Avoid using pointers directly, and instead use references. Also
std::array over traditional arrays. However, there are situations where
pointers and traditional arrays are the only/better choice, and in those cases use
const qualification correctly to maximize safety.
Return-value optimization is a compiler technique to avoid copying an object that a function returns as its value, including avoiding creation of a temporary object. This optimization permits a function to efficiently return large objects while also simplifying the function’s interface and eliminating scope for issues such as resource leaks. However, there are situations where a compiler may be unable to perform this optimization, where a function does not capitalize on the optimization, and where it may be acceptable or even be better to forego this optimization.More >>
This post presents detailed guidelines for using
std::string_view. It includes a total
of 21 guidelines, grouped into five categories.
To insert a line break into an output stream, just insert the newline character (
instead of inserting
std::endl. For example, write
std::cout << "hello world" << '\n';
instead of writing
std::cout << "hello world" << std::endl;.
This is Part 2 of a 3-part series on
std::string_view. This part focuses on the safety
std::string_view provides over character arrays, and on the safety considerations to be
made when using
Introduced in C++17, the STL class
std::string_view provides more efficient ways than
std::string to process immutable (read only) text data. It also provides a safer means
to perform read-only operations on character arrays. Overall, using
read-only operations on text data can improve execution speed as well as reduce both
main-memory usage and executable size. It can also make programs safer and more
This is Part 1 of a 3-part series on
std::string_view. This part focuses on efficiency
std::string. Part 2
focuses on safety. Part 3 provides guidelines for
This post discusses the use of C-strings in C++. It defines the terms C-string and NTBS (null-terminated byte string); discusses C-string literals and variables; outlines common patterns of C-string usage; and highlights a subtle technical difference between C-strings and NTBSs.
But first, some advice: Avoid using C-strings in C++, and instead use
possible. However, there are situations where C-strings can provide better performance
std::string, but make that choice on a case-by-case basis. Even when using a
C-string, consider using it with the light-weight wrapper