c++

Understanding when not to std::move in C++

Understanding when not to std::move in C++

One of the most important concepts introduced in C++11 was move semantics. Move semantics is a way to avoid expensive deep copy operations and replace them with cheaper move operations. Essentially, you can think of it as turning a deep copy into a shallow copy.

Move semantics came along with several more or less related features, such as rvalue references, xvalues, forwarding  references, perfect forwarding, and so on. The standard C++ library gained a function template called std::move, which, despite its name, does not move anything. std::move merely casts its argument to an rvalue reference to allow moving it, but doesn’t guarantee a move operation. For example, we can write a more effective version of swap using std::move:

template<typename T>
void swap(T& a, T& b)
{
  T t(std::move (a));
  a = std::move (b);
  b = std::move (t);
}

This version of swap consists of one move construction and two move assignments and does not involve any deep copies. All is well. However, std::move must be used judiciously; using it blithely may lead to performance degradation, or simply be redundant, affecting readability of the code. Fortunately, the compiler can sometimes help with finding such wrong uses of std::move. In this article, I will introduce two new warnings I’ve implemented for GCC 9 that deal with incorrect usage of std::move.

Continue reading “Understanding when not to std::move in C++”

Share
Report from the February 2019 ISO C++ meeting (Core Language working group)

Report from the February 2019 ISO C++ meeting (Core Language working group)

The February 2019 ISO C++ meeting was held in Kailua-Kona, Hawaii. As usual, Red Hat sent three developers to the meeting: I attended in the Core Language working group, Jonathan Wakely in Library, and Thomas Rodgers in SG1 (parallelism and concurrency). The meeting went smoothly, although there was significant uncertainty at the beginning where we would end up. In the end, Modules and Coroutines were accepted into the C++20 draft, so now we have our work cut out for us nailing down the remaining loose corners. Here ar highlights from the meeting.

Continue reading “Report from the February 2019 ISO C++ meeting (Core Language working group)”

Share
A look at LLVM Advanced Data Types and trivially copyable types

A look at LLVM Advanced Data Types and trivially copyable types

A few bugs have been lurking in the LLVM Bugzilla for a long time, namely #39427 and #35978, which are related to a custom implementation of the is_trivially_copyable data type, and they have a bad impact on the Application Binary Interface (ABI) of LLVM libraries. In this article, I will take a closer look at these issues and describe potential workarounds.

The LLVM compiler infrastructure relies on several Advanced Data Types (ADT) to provide different speed/size trade-offs than the containers from the Standard Template Library (STL). Additionally, this ADT library provides features from future standard versions, but implemented in the C++ version (currently C++11) that LLVM supports as a code base. Finally, these ADTs must be compatible with the compiler requirements of the LLVM code base; basically, GCC version >= 4.8 and Clang version >= 3.1. (If you are interested in LLVM ADTs, Chandler Carruth did a nice talk on the subject at CppCon 2016.)

Continue reading “A look at LLVM Advanced Data Types and trivially copyable types”

Share
November 2018 ISO C++ meeting trip report (Core Language)

November 2018 ISO C++ meeting trip report (Core Language)

The ISO C++ standards meeting in November 2018 was held in San Diego, CA. As usual, Red Hat sent three of us to the meeting: me (for the Core Language Working Group), Jonathan Wakely (for the Library Working Group [LEWG]), and Thomas Rodgers (for the Concurrency and Parallelism Study Group [SG1]). I felt the meeting was productive, though some features that had been expected to make it into C++20 are now in question.

Here are new C++ features accepted at the meeting:

Continue reading “November 2018 ISO C++ meeting trip report (Core Language)”

Share
Fall 2018 ISO WG21 C++ Standards Committee meeting trip report

Fall 2018 ISO WG21 C++ Standards Committee meeting trip report

The fall C++ meeting was held in San Diego, CA. As usual, Red Hat sent three of us to the meeting: myself from the Concurrency and Parallelism Study Group (SG1), Jason Merrill from the Core Language Working Group, and Jonathan Wakely from the Library Working Group (LEWG).

SG1 had a fairly full plate but finished the week with a bit of breathing room to spare. This article describes the major topics discussed this week in SG1.

Continue reading “Fall 2018 ISO WG21 C++ Standards Committee meeting trip report”

Share
Processing CloudEvents with Eclipse Vert.x

Processing CloudEvents with Eclipse Vert.x

Our connected world is full of events that are triggered or received by different software services. One of the big issues is that event publishers tend to describe events differently and in ways that are mostly incompatible with each other.

To address this, the Serverless Working Group from the Cloud Native Computing Foundation (CNCF) recently announced version 0.2 of the CloudEvents specification. The specification aims to describe event data in a common, standardized way. To some degree, a CloudEvent is an abstract envelope with some specified attributes that describe a concrete event and its data.

Working with CloudEvents is simple. This article shows how to use the powerful JVM toolkit provided by Vert.x to either generate or receive and process CloudEvents.

Continue reading “Processing CloudEvents with Eclipse Vert.x”

Share
How to call the OpenShift REST API from C#

How to call the OpenShift REST API from C#

When you want to do automated tasks for builds and deployments with Red Hat OpenShift, you might want to take advantage of the OpenShift REST API. In scripts you can use oc CLI command which talks to the REST APIs. However there are times when it is more convenient to do this directly from your C# code without having to invoke an external program. This is the value of having an infrastructure platform that is exposed as services with an open API.

If you want to call the API from your C# code, you have to create a request object, call the API, and parse the response object. The upstream project, OpenShift Origin, provides a Swagger 2.0 specification and you can generate a client library for each programming language. Of course, C# is supported.  This isn’t a new approach, Kubernetes has a repository that is generated by Swagger Codegen.

For C#, we can use Microsoft Visual Studio to generate a C# client library for a REST API. In this article, I’ll walk you through the process of generating the library from the definition.

Continue reading “How to call the OpenShift REST API from C#”

Share
June 2018 ISO C++ Meeting Trip Report (Core Language)

June 2018 ISO C++ Meeting Trip Report (Core Language)

The Summer 2018 ISO C++ standards committee meeting this year was back in Rapperswil, Switzerland. The new features for C++2a are coming fast now; the Core language working group had very little time for issue processing because of all the proposal papers coming to us from the Evolution working group.

Red Hat sent three of us to the meeting, to cover different tracks: myself (Core), Jonathan Wakely (Library), and Torvald Riegel (Parallelism/Concurrency).  Overall, I thought the meeting was very successful; we made significant progress in a lot of areas.

New C++ language features that were accepted at this meeting:

Continue reading “June 2018 ISO C++ Meeting Trip Report (Core Language)”

Share
Detecting String Truncation with GCC 8

Detecting String Truncation with GCC 8

Continuing in the effort to detect common programming errors, the just-released GCC 8 contains a number of new warnings as well as enhancements to existing checkers to help find non-obvious bugs in C and C++ code. This article focuses on those that deal with inadvertent string truncation and discusses some of the approaches for avoiding the underlying problems. If you haven’t read it, you might also want to read David Malcolm’s article Usability improvements in GCC 8.

To use GCC 8 on RHEL, see How to install GCC 8 and Clang/LLVM 6 on Red Hat Enterprise Linux 7.  GCC 8 is the default compiler in Red Hat Enterprise Linux 8 Beta.

Why Is String Truncation a Problem?

It is well-known why buffer overflow is dangerous: writing past the end of an object can overwrite data in adjacent storage, resulting in data corruption. In the most benign cases, the corruption can simply lead to incorrect behavior of the program. If the adjacent data is an address in the executable text segment, the corruption may be exploitable to gain control of the affected process, which can lead to a security vulnerability. (See CWE-119 for more on buffer overflow.)

Continue reading “Detecting String Truncation with GCC 8”

Share