Articles & Books

Q&A - C++ Initialization -- Bartlomiej Filipek

Last time I showed a couple of questions about initialization. Try them here if you haven’t already. In this article, I’ll show you the answers and add more notes about initialization in C++.

Q&A - C++ Initialization

by Bartlomiej Filipek

From the article:

Will this code work in C++11?
struct User { std::string name = "unknown"; unsigned age { 0 }; };
User u { "John", 101 };
  1. Yes, the code compiles in C++11 mode.
  2. The code compiles starting with C++14 mode.
  3. The code doesn’t compile even in C++20.

The Obvious Final Step -- Andrzej Krzemieński

During the construction of an XML file when you write an element, it is obvious that the last thing that you do is to write the closing tag. By obvious we mean: writing it down adds no new information (there is no other possible final instruction for this task), and it would be a bug if this instruction wasn’t there.

The Obvious Final Step

by Andrzej Krzemieński

From the article:

The title may be misleading, as I had to invent a new short term for the pattern that occurs in the code once in a while. Example first:

// write an element of an XML file

xml.begin_element("port");
xml.attribute("name", name);
xml.attribute("location", loc);
xml.end_element("port");  // <-- the obvious final step

Defining Interfaces in C++: Concepts Versus Inheritance -- Daniel Lemire

DanielLemire.pngIn a previous blog post, Daniel Lemire showed how you could define ‘an interface’ in C++ using concepts and stated that he did not take into account inheritance as a strategy. He does so here.

Defining Interfaces in C++: Concepts Versus Inheritance

by Daniel Lemire

From the article:

In a previous blog post, I showed how you could define ‘an interface’ in C++ using concepts. For example, I can specify that a type should have the methods has_next, next and reset:

template <typename T>
concept is_iterable = requires(T v) {
                        { v.has_next() } -> std::convertible_to<bool>;
                        { v.next() } -> std::same_as<uint32_t>;
                        { v.reset() };
                      };

The Case of string_view and the Magic String -- Giovanni Dicanio

Someone learned about std::string_view, and started replacing instances of std::string const& with string_views in their C++ code base. As a result of that, a subtle bug shows up!

The Case of string_view and the Magic String

by Giovanni Dicanio

From the article:

(...) The code is recompiled and executed. But, unfortunately, now the output has changed! What’s going on here? Where does that “magic string” come from?

Sign Up for the free Pure Virtual C++ 2023 Conference -- Sy Brand

PureVirtualC++.pngEvery year we run Pure Virtual C++: a free one-day virtual conference for the whole C++ community. Next month we’re doing it again! Sign-up for free to get access to our five live sessions and a host of pre-conference content.

Sign Up for the free Pure Virtual C++ 2023 Conference

by Sy Brand

From the article:

The live event will run June 6th 13:00-16:00 UTC. Videos will be available to stream for free on YouTube after the conference.

The live sessions will be:

  • C++ Compiler Errors for Humans with Sy Brand
  • Address Sanitizer continue_on_error with Jim Radigan
  • Value-Oriented Programming with Tony Van Eerd
  • Productive Cross-Platform and Game Development in Visual Studio with Sinem Akinci and David Li
  • Build Time Reflection with C++ in Year 2023 with Gabriel Dos Reis

 

Defining Interfaces in C++ with ‘Concepts’ (C++20) -- Daniel Lemire

In an earlier blog post, I showed that the Go programming language allows you to write generic functions once you have defined an interface.

Defining Interfaces in C++ with ‘Concepts’ (C++20)

by Daniel Lemire

From the article:

Java has a very similar concept under the same name (interface). I gave the following example:


	type IntIterable interface {

	    HasNext() bool

	    Next() uint32

	    Reset()

	}

	

	func Count(i IntIterable) (count int) {

	    count = 0

	    i.Reset()

	    for i.HasNext() {

	        i.Next()

	        count++

	    }

	    return

	}

Consider Using constexpr Static Function Variables for Performance in C++ -- Daniel Lemire

When programming, we often need constant variables that are used within a single function.

Consider Using constexpr Static Function Variables for Performance in C++

by Daniel Lemire

From the article:

When programming, we often need constant variables that are used within a single function. For example, you may want to look up characters from a table. The following function is efficient:

char table(int idx) {
  const char array[] = {'z', 'b', 'k', 'd'};
  return array[idx];
}

ISO C++ Feb 2023 meeting trip report (core language) -- Jason Merrill

The C++ committee had its second hybrid meeting in Issaquah, WA in February, to finalize C++23. This was also the venue where we finished up C++14.

ISO C++ Feb 2023 meeting trip report (core language)

by Jason Merrill

From the article:

The C++ committee had its second hybrid meeting in Issaquah, WA in February, to finalize C++23.  This was also the venue where we finished up C++14.  I was there in person from Red Hat, while Thomas Rodgers and Jonathan Wakely attended virtually.  As usual, I spent most of my time in the Core working group.

The primary goal of the meeting was to finish responding to national body comments on the draft and advance the result for balloting to become C++23, and indeed we did; the week went smoothly from my perspective.  We also spent a fair amount of time reviewing papers and issues that were not expected to go into C++23.

Most of the C++23 fixes at this meeting were unremarkable, but a couple are worth mentioning:

CWG2518 clarifies that...

Functional Programming in C++ -- John Carmack

A classic, over a decade old and worth making the rounds again:

Functional Programming in C++

by John Carmack

From the article:

My pragmatic summary: A large fraction of the flaws in software development are due to programmers not fully understanding all the possible states their code may execute in. ... No matter what language you work in, programming in a functional style provides benefits.  You should do it whenever it is convenient, and you should think hard about the decision when it isn’t convenient.

... C++ doesn’t encourage functional programming, but it doesn’t prevent you from doing it, and you retain the power to drop down and apply SIMD intrinsics to hand laid out data backed by memory mapped files, or whatever other nitty-gritty goodness you find the need for. ... 

GCC 13.1 Released

The GCC developers are proud to announce a new major GCC release, 13.1.

GCC 13.1 Released

by Richard Biener

From the announcement:

The C frontend got support for several C23 features, the C++ frontend for C++23 features.  The C++ standard library experimental support for C++20 and C++23 was enhanced.  For the C family of languages you can now use -fstrict-flex-arrays[=level] to control the behavior for the various legacy forms of specifying flexible array members.