basics

Quick Q: What does the explicit keyword mean?

Quick A: It tell the compiler not to do any implicit conversions of types.

Recnetly on SO:

What does the explicit keyword mean?

The compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameter to convert from one type to another in order to get the right type for a parameter.

Here's an example class with a constructor that can be used for implicit conversions:

class Foo
{
public:
  // single parameter constructor, can be used as an implicit conversion
  Foo (int foo) : m_foo (foo)
  {
  }

  int GetFoo () { return m_foo; }

private:
  int m_foo;
};

Here's a simple function that takes a Foo object:

void DoBar (Foo foo)
{
  int i = foo.GetFoo ();
}

and here's where the DoBar function is called.

int main ()
{
  DoBar (42);
}

The argument is not a Foo object, but an int. However, there exists a constructor for Foo that takes an int so this constructor can be used to convert the parameter to the correct type.

The compiler is allowed to do this once for each parameter.

Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class will create a compiler error at the function call DoBar (42). It is now necessary to call for conversion explicitly with  DoBar (Foo (42))

The reason you might want to do this is to avoid accidental construction that can hide bugs. Contrived example:

  • You have a MyString(int size) class with a constructor that constructs a string of the given size. You have a function print(const MyString&), and you call print(3) (when you actually intended to call print("3")). You expect it to print "3", but it prints an empty string of length 3 instead.

How not_null can improve your code?—Bartlomiej Filipek

Let’s investigate what "not_null" (from the Core Guidelines/Guideline Support Library) can do for us.

How not_null can improve your code?

by Bartlomiej Filipek

From the article:

I believe "not_null" can help in many places. It won’t do the magic on its own, but at least it forces us to rethink the design. Functions might become smaller (as they won’t have to check for nulls), but on the other hand, the caller might require being updated.

Keynotes at Meeting C++ 2017

With the conference just a few weeks away, an update on the 3 awesome keynotes of this years Meeting C++:

Keynotes at Meeting C++ 2017

by Jens Weller

From the article:

Are you excited for Meeting C++ 2017?!? I quickly wanted to give an update on the 3 keynotes at the conference this year! Each day will feature one keynote, where the first two are in the morning, while the Closing Keynote is kind of the last thing to happen before the closing message. Also, all 3 keynote speakers have now (finally) their speaker profile.

Static libs do not modular make—Thomas Young

An article about static libraries, the benefits (or non-benefits) of splitting your project into static libraries, and the knock-on effects this can have on project dependencies.

Static libs do not modular make

by Thomas Young

From the article:

A cautionary tale about statically-linked libraries, as generated by C/C++ build tools.

As a project accumulates features, and complexity, it gets harder to understand exactly what's going on, and to find your way around the source code. You need to find some way to organise the code and try and keep things manageable.

A common idea, in this situation, is to group some source files together to split out as a static library.

I'm going to argue that this actually does very little, in itself, to increase modularity, can have the effect of significantly increasing dependencies, and is maybe not such a good idea, after all.

Volunteer at Meeting C++ 2017

For the first time ever, you can participate in Meeting C++ as a volunteer, just like you could do with CppCon!

Volunteering at Meeting C++ 2017

by Jens Weller

From the article:

Something new, which didn't exist in the last 5 editions of the Meeting C++ conference. You are now able to become a volunteer at Meeting C++ 2017, like you can and always could do for CppCon.

Historically the staff for Meeting C++ first was from my own C++ User Group...