(Not) using namespace std;

using namespace std;

This is called a using directive. Using it in code that is supposed to work for years is not a good idea.

Sometimes programmers are given advice to just type using namespace std; and all things from the Standard Library can be used easily, without bothering with namespaces. This is a good advice when you are learning C++ and when you are doing toy examples, or write small private programs. Because for these programs it is not a big deal if they cease to compile once you change the compiler or language version. Continue reading

Advertisements
Posted in programming | Tagged | 8 Comments

Deducing your intentions

The language feature in C++17 known as class template argument deduction was intended to supersede factory functions like make_pair, make_tuple, make_optional, as described in p0091r2. This goal has not been fully achieved and we may still need to stick to make_ functions. In this post we will briefly describe what class template argument deduction is, and why it works differently than what people often expect. Continue reading

Posted in programming | Tagged , , | 12 Comments

Treating symptoms instead of the cause

Compiler warnings can sometimes help us find bugs before we even build our program, but it only works provided that we can make use of the warnings. Especially, when we are aware that warnings detect only symptoms of the bugs rather than bugs themselves. Continue reading

Posted in programming | Tagged , , | 7 Comments

Faces of undefined behavior

I have been busy recently (doing C++-related stuff) and cannot find a spare time for preparing a decent blog post. I expect that to change in November. For the interim I am posting here my last year’s talk at code::dive: Faces of undefined behavior. It is relevant in the context of recent discussions about contracts in C++ and go-back-in-time optimizations.

Posted in programming | Tagged , , , , , | 1 Comment

Functions in std

Names of functions from the Standard Library used as pointers/references to functions can cause breakage in your code if you are upgrading to the newer version of C++ or compiling using a different implementation of the Standard Library, or even when just adding headers. In this post we will see why this is so, and what we can do about it.
Continue reading

Posted in programming | Tagged , | 9 Comments

Rvalues redefined

In this post we will explore what I consider the most significant language change in C++17. I call it the most significant because it changes the way you design your resource-managing types and how you think about initialization. It is often called “guaranteed copy elision”, but I will not use that name (except for this single time) because it does not reflect what this feature is. C++ has completely changed the meaning of rvalue (actually, prvalue). Continue reading

Posted in programming | Tagged , | 28 Comments

Testing C++ online

In this short post I wanted to share links to three online services that I use for testing different aspects of C++ and C++ compilers.

  • https://wandbox.org/ — this allows you to compile and observe the output of programs compiled in different versions of GCC and Clang, with different versions of Boost libraries;
  • https://godbolt.org/ — this allows you to see the assembly of C++ programs generated with different compilers on different platforms;
  • http://quick-bench.com/ — this allows you to micro-benchmark C++ functions on different versions of GCC and Clang using Google’s benchmark library.
Posted in programming | 3 Comments