Blog

doctest—the lightest feature rich C++ single header testing framework version 1.0.0 released!

The lightest feature rich C++ single header testing framework for unit tests and TDD

doctest 1.0.0 released!

From the release:

The doctest library is inspired by the unittest {} functionality of the D programming language and Python's docstrings - tests can be considered a form of documentation and should be able to reside near the production code which they test.

It is modeled after Catch which is currently the most popular and easy to use alternative for testing in C++

Quick Q: Is final used for optimization in C++?

Quick A: Possibly.

Recently on SO:

Is final used for optimization in C++?

It can be.

An optimisation along these lines would relate to the "de-virtualization" of the virtual calls. This is not always immediately affected by the final of the class nor method. Albeit they offer help to determine this, the normal rules of the virtual functions and class hierarchy apply.

If the compiler can determine that at runtime a particular method will always be called (e.g. given the OP example, with an automatic object), it could apply such an optimisation anyway, irrespective of whether the method is final or not.

Optimisations fall under the as-if rule, that allow the compiler to apply any transformation so long as the observable behaviour is as-if the original code had been executed.

C++ Modules - Spring Update—Gabriel Dos Reis

Let's talk about modules!

C++ Modules - Spring Update

by Gabriel Dos Reis

Abstract

“Modules” are a frequently requested and long-awaited feature by C++ programmers. The basic idea is a direct language support for (a) expressing the boundaries and dependencies of program components; (b) isolating source codes from macro vagaries; (c) scaling compile time, especially for large projects, given the ubiquity of “headers-only” template libraries; (d) spur innovation and deployment of semantics-aware developer tools. I will give an overview of the design points, goals, with emphasis on how modules address the four major problems mentioned above. I will also address standardization process and implementation issues

CppCon 2015 What’s New in Visual C++ 2015 and Future Directions—Steve Carroll • Ayman Shoukry

Have you registered for CppCon 2016 in September? Don’t delay – Early Bird registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

What's New in Visual C++ 2015 and Future Directions

by Steve Carroll • Ayman Shoukry

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

In this talk, we'll discuss new features, optimizations, and scenarios in Visual Studio 2015. We'll cover new backend optimizations, improved build throughput, new productivity and diagnostics features, and give a detailed update on our conformance progress, as well as talk about cool new c++1y features that we are shipping from await to modules.

Visual Studio isn't just for Microsoft platform developers. We'll also demonstrate our latest cross platform C++ development features for Android and iOS. We'll also give a sneak peak at our work on combining the Clang frontend with our existing backend to bring Clang support for Windows to Visual Studio.

CppCast Episode 57: Runtime Compiled C++ with Doug Binks

Episode 57 of CppCast the only podcast for C++ developers by C++ developers. In this episode Rob and Jason are joined by Doug Binks from Enkisoftware to discuss Runtime Compile C++.

CppCast Episode 57: Runtime Compiled C++ with Doug Binks

by Rob Irving and Jason Turner

About the interviewee:

Doug Binks is programming the game Avoyd using Runtime Compiled C++, a technique he co-developed with industry friends; and enkiTS, a lightweight task scheduler.

An experienced game developer, Doug was previously Technical Lead of the Game Architecture Initiative at Intel. He has worked in the games industry in roles ranging from the R&D development manager at Crytek to head of studio at Strangelite, as well as lead programmer. An early interest in games development was sidetracked by a doctorate in Physics at Oxford University, and two post-doctoral posts as an academic researcher in experimental nonlinear pattern formation, specializing in fluid mechanics. His fondest childhood memories are of programming games in assembly on the ZX81.

Quick Q: was raw-pointer constructor of shared_ptr a mistake?

Quick A: No, its usage is well defined.

Recently on SO:

was raw-pointer constructor of shared_ptr a mistake?

In hindsight, given make_shared, would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11?
What if you don't control the allocation of the object? What if you need to use a custom deleter? What if you need list-initialization instead of parens?

None of these cases is handled by make_shared.

Additionally, if you're using weak_ptr, a shared_ptr allocated via make_shared won't free any memory until all the weak_ptrs are destroyed as well. So even if you have a normal shared pointer where none of the above apply, it's possible that you may still prefer the raw pointer constructor.

Yet another situation would be if your type provides overloads for operator new and operator delete. These may make it ill-suited for make_shared, since those overloads will not be called - and presumably they exist for a reason.

CppCon 2015 C++ Multi-dimensional Arrays…—Pramod Gupta

Have you registered for CppCon 2016 in September? Don’t delay – Early Bird registration is open now.

While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2015 for you to enjoy. Here is today’s feature:

C++ Multi-dimensional Arrays...

by Pramod Gupta

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

The language feature of passing a multi-dimensional array to a function without specifying all its dimensions at compile time is crucial for computational physics and applied mathematics. For example a matrix is a two dimensional array and a matrix inversion function which needs to know the size of the matrix at compile time would be of limited use. Major general purpose languages such as C, Java and C# support this feature. Of course, scientific programming languages like Fortran, Matlab and R also support this feature.

C++ is perhaps the only major programming language which does not allow passing a multi-dimensional array to a function unless the size of all the dimensions except the first one is known at compile time. Due to this limitation of C++, various libraries have been developed for using multi-dimensional arrays in C++. Some of these libraries are Blitz++, Armadillo, Eigen and boost.multi_array. These libraries are very large and complex. While they do provide a wide variety of features, they have a learning curve which may be difficult to justify for something as basic as passing multi-dimensional arrays to functions. Also the computational physics or applied mathematics code becomes dependent on a large non-standard library. Hence its usage will be limited to only those scientists who are willing to install these non-standard libraries.

The reference feature of C++ allows us to develop a multi-dimensional array class. The class has a small number of lines of code and hence the code can be included with the scientific application code. We use this class to write programs for various areas of computational physics and show that the class is easy to use and it leads to readable programs.

Quick Q: What makes moving objects faster than copying?

Quick A: it allows to steal ressources instead of creating new ones.

Recently on SO:

What makes moving objects faster than copying?

It's all about implementation. Consider simple string class:

class my_string {
  char* ptr;
  size_t capacity;
  size_t length;
};

Semantics of copy requires us to make a full copy of string including allocation of another array in dynamic memory and copying *ptr contents there, which is expensive.

Semantics of move requires us only to transfer the value of pointer itself to new object without duplicating contents of string.

If, of course, class doesn't use dynamic memory or system resources, then there is no difference between moving and copying in terms of performance.

Quick Q: Why assigning a value is calling the implicit constructor?

Quick A: Because the default assignement operator knows only the type of your object, which it can get be calling the constructor.

Recently on SO:

What is the point of calling constructor with implicit conversion instead of assignment operator after an object is initalized?

There is no implicit CBox::operator=(double), so box = 2.0; has to create a temporary CBox object. It's equivalent to box = CBox(2.0);.

Making your constructor explicit disallows the implicit conversion from double to CBox, so no appropriate assignment operator exists, and you get a compile error.

Quick Q: Why is there no to_string(const string&)?

Quick A: If you need such a function you can create it.

Recently on SO:

Why is there no to_string(const string&)?

You can just write your own templated function with proper overloads as follows:

#include <iostream>
#include <string>
using namespace std;

template<typename T>
std::string toString(const T& t) {
    return std::to_string(t);
}

std::string toString(const char* t) {
    return t;
}

std::string toString(const std::string& t) {
    return t;
}

int main() {
    cout << toString(10) << endl;
    cout << toString(1.5) << endl;
    cout << toString("char*") << endl;
    cout << toString(std::string("string")) << endl;
    return 0;
}