Semantics in modern C++

Old No Comments on Semantics in modern C++ 13

Modern C++ attempts to provide a clear, flexible and safer than ever code. The new language semantics have provided a way for C++ code to give the advantages of fast, flexible development of languages like C# and Java combined with the native performance of C++.

Some of the features in modern C++ are:

  • Constructor delegation

Typically used by Java programmers, which involves calling a constructor from another constructor so that common initialization tasks can be done efficiently without duplication of code.

Here’s an example:

class Foo
{
public:
  Foo();
  Foo(int val);
private:
  int bar;
};

// bar will be initialized to zero.
Foo::Foo() : Foo(0) {}

// bar will be initialized to val.
Foo::Foo(int val) : bar(val) {}
  • Automatic type deduction

Ever stuck calling a function and then figuring out what its return type is? Well, no more. Just specify auto as the type name and the compiler will do the rest for you.

Here’s an example:

Suppose this is the function definition:

double mean(const int array[], const int length)
{
  double sum=0.0;
  for(int i=0; i<length; i++)
  {
    sum+=array[i];
  }
  return sum/length;
}

Somewhere in your main program, you can have:

int array[]={1, 2, 3, 4, 5};
auto avg=mean(array, 5);
  • Lambda functions

One of the most anticipated features, lambda functions are essentially anonymous functions which allow you to write a block of code which is supposed to do a task, but might be called only once, for which declaring a function seems stupid. Extensively used with the STL to replace functors.

// collection may be any STL collection.
// Here, it stores a collection of string objects to be processed.
for_each( begin(collection), 
          end(collection), 
          []( string& s )
{
  // Required logic here...
} );
  • Move on return of value types

Consider the following three scenarios:

  1. Return an object by value and the copy operations bug the CPU.
  2. Return an object by reference on the heap and the pointer semantics bug you.
  3. Return an object by reference on the stack and the object is no more when you access it later.

Modern C++ has now introduced the concept of move of value types which basically combines the benefits of all the three scenarios above. You can now return stack objects from a function and not worry about the deep copy involved, because the whole object being returned is simply moved from the function’s scope to the caller’s scope. Thus, you get to write your program with normal object semantics instead of pointer semantics, while getting the benefits of heap-based allocation.

The only extra thing needed is to write appropriate move constructor and corresponding assignment operator overload function.

  • Automatic object management

The ending curly brace just isn’t for readability. It now automatically disposes off objects in its scope by calling the required destructors when the scope is going to end. Thus, it frees you from having to always keep a track of objects to avoid leaking memory space.

  • Contiguous allocation

The STL vector class now stores objects in a contiguous manner, instead of pointers pointing to different locations, which enables it to offer the performance of native arrays while being dynamic in size.

Author

Vivek Prajapati

A moderate level programmer interested in administration and Arduino. Familiar with C++, Java, PHP, C# with my favourite being C++. Just finished my bachelor's degree in IT.

Related Articles

Leave a comment

Back to Top