Lokasi ngalangkungan proxy:   
[Ngawartoskeun bug]   [Panyetelan cookie]                

sigcpp

Special Interest Group on C++

  • 12 Oct 2020

    Ponder runtime efficiency

    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 >>
  • 10 Jul 2020

    Variadic print macros

    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 >>
  • 20 Jun 2020

    What the actual const?

    This post discusses the effect of const qualifications in pointer declarations, specifically the distinction between pointer constness and data constness. It first examines const qualifications of pointers to non-array data, and then examines const 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 pointers.

    This post is motivated by the observation that those new to pointers tend to mistake data 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 prefer 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.

    More >>
  • 08 Jun 2020

    Return value optimization (RVO)

    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 >>
  • 20 Apr 2020

    Guidelines for processing immutable text

    This post presents detailed guidelines for using std::string_view. It includes a total of 21 guidelines, grouped into five categories.

    This post concludes the 3-part series on processing immutable text. Part 1 of the series focuses on efficiency aspects of processing immutable text. Part 2 focuses on safety.

    More >>
  • 18 Apr 2020

    Avoid using std::endl to insert line breaks

    To insert a line break into an output stream, just insert the newline character ('\n') instead of inserting std::endl. For example, write std::cout << "hello world" << '\n'; instead of writing std::cout << "hello world" << std::endl;.

    More >>
  • 07 Apr 2020

    Safely processing immutable text

    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 std::string_view.

    Part 1 focuses on efficiency of std::string_view over std::string. Part 3 provides guidelines for using std::string_view.

    More >>
  • 03 Apr 2020

    Efficiently processing immutable text

    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 std::string_view for 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 maintainable.

    This is Part 1 of a 3-part series on std::string_view. This part focuses on efficiency of std::string_view over std::string. Part 2 focuses on safety. Part 3 provides guidelines for using std::string_view.

    More >>
  • 30 Mar 2020

    Exploring C-strings

    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 std::string where possible. However, there are situations where C-strings can provide better performance over 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 std::string_view.

    More >>