EzDevInfo.com

c++11 interview questions

Top c++11 frequently asked interview questions

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

C++11 introduced a standardized memory model, but what exactly does that mean? And how is it going to affect C++ programming?

Herb Sutter says here that,

The memory model means that C++ code now has a standardized library to call regardless of who made the compiler and on what platform it's running. There's a standard way to control how different threads talk to the processor's memory.

"When you are talking about splitting [code] across different cores that's in the standard, we are talking about the memory model. We are going to optimize it without breaking the following assumptions people are going to make in the code," Sutter said.

Well, I can memorize this and similar paragraphs available online (as I've my own memory model since birth :P) and can even post as answer to questions asked by others, but to be honest, I don't exactly understand this.

So, what I basically want to know is, C++ programmers used to develop multi-threaded applications even before, so how does it matter if its POSIX threads, or Windows threads, or C++11 threads? What are the benefits? I want to understand the low-level details.

I also get this feeling that the C++11 memory model is somehow related to C++11 multi-threading support, as I often see these two together. If it is, how exactly? Why should they be related?

As I don't know how internals of multi-threading works, and what memory model means in general, please help me understand these concepts. :-)


Source: (StackOverflow)

Are the days of passing const std::string & as a parameter over?

I heard a recent talk by Herb Sutter who suggested that the reasons to pass std::vector and std::string by const & are largely gone. He suggested that writing a function such as the following is now preferable:

std::string do_something ( std::string inval )
{
   std::string return_val;
   // ... do stuff ...
   return return_val;
}

I understand that the return_val will be an rvalue at the point the function returns and can therefore be returned using move semantics, which are very cheap. However, inval is still much larger than the size of a reference (which is usually implemented as a pointer). This is because a std::string has various components including a pointer into the heap and a member char[] for short string optimization. So it seems to me that passing by reference is still a good idea.

Can anyone explain why Herb might have said this?


Source: (StackOverflow)

Advertisements

What are Aggregates and PODs and how/why are they special?

This FAQ is about Aggregates and PODs and covers the following material:

  • What are Aggregates?
  • What are PODs (Plain Old Data)?
  • How are they related?
  • How and why are they special?
  • What changes for C++11?

Source: (StackOverflow)

What are inline namespaces for?

C++11 allows inline namespaces, all members of which are also automatically in the enclosing namespace. I cannot think of any useful application of this -- can somebody please give a brief, succinct example of a situation where an inline namespace is needed and where it is the most idiomatic solution?

(Also, it is not clear to me what happens when a namespace is declared inline in one but not all declarations, which may live in different files. Isn't this begging for trouble?)


Source: (StackOverflow)

What does T&& (double ampersand) mean in C++11?

I've been looking into some of the new features of C++11 and one I've noticed is the double ampersand in declaring variables, like T&& var.

For a start, what is this beast called? I wish Google would allow us to search for punctuation like this.

What exactly does it mean?

At first glance, it appears to be a double reference (like the C-style double pointers T** var), but I'm having a hard time thinking of a use case for that.


Source: (StackOverflow)

What is a lambda expression in C++11?

What is a lambda expression in C++11? When would I use one? What class of problem do they solve that wasn't possible prior to their introduction?

A few examples, and use cases would be useful.


Source: (StackOverflow)

What is a smart pointer and when should I use one?

What is a smart pointer and when should I use one?


Source: (StackOverflow)

What are move semantics?

I just finished listening to the Software Engineering radio podcast interview with Scott Meyers regarding C++0x. Most of the new features made sense to me, and I am actually excited about C++0x now, with the exception of one. I still don't get move semantics... What are they exactly?


Source: (StackOverflow)

When should you use constexpr capability in C++11?

It seems to me that having a "function that always returns 5" is breaking or diluting the meaning of "calling a function". There must be a reason, or a need for this capability or it wouldn't be in C++11. Why is it there?

// preprocessor.
#define MEANING_OF_LIFE 42

// constants:
const int MeaningOfLife = 42;

// constexpr-function:
constexpr int MeaningOfLife () { return 42; }

It seems to me that if I wrote a function that return a literal value, and I came up to a code-review, someone would tell me, I should then, declare a constant value instead of writing return 5.


Source: (StackOverflow)

C++ template typedef

I have a class

template<size_t N, size_t M>
class Matrix {
    // ....
};

I want to make a typedef which creates a Vector (column vector) which is equivalent to a Matrix with sizes N and 1. Something like that:

typedef Matrix<N,1> Vector<N>;

Which produces compile error. The following creates something similar, but not exactly what I want:

template <int N>
class Vector: public Matrix<N,1>
{ };

Is there a solution or a not too expensive workaround / best-practice for it?


Source: (StackOverflow)

What are rvalues, lvalues, xvalues, glvalues, and prvalues?

In C++03, an expression is either an rvalue or an lvalue.

In C++11, an expression can be an:

  1. rvalue
  2. lvalue
  3. xvalue
  4. glvalue
  5. prvalue

Two categories have become five categories.

  • What are these new categories of expressions?
  • How do these new categories relate to the existing rvalue and lvalue categories?
  • Are the rvalue and lvalue categories in C++0x the same as they are in C++03?
  • Why are these new categories needed? Are the WG21 gods just trying to confuse us mere mortals?

Source: (StackOverflow)

Why should I use a pointer rather than the object itself?

I'm coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves, for example this declaration:

Object *myObject = new Object;

rather than:

Object myObject;

Or instead of using a function, let's say testFunc(), like this:

myObject.testFunc();

we have to write:

myObject->testFunc();

But I can't figure out why should we do it this way. I would assume it has to do with efficiency and speed since we get direct access to the memory address. Am I right?


Source: (StackOverflow)

How do I pass a unique_ptr argument to a constructor or a function?

I'm new to move semantics in C++11 and I don't know very well how to handle unique_ptr parameters in constructors or functions. Consider this class referencing itself:

#include <memory>

class Base
{
  public:

    typedef unique_ptr<Base> UPtr;

    Base(){}
    Base(Base::UPtr n):next(std::move(n)){}

    virtual ~Base(){}

    void setNext(Base::UPtr n)
    {
      next = std::move(n);
    }

  protected :

    Base::UPtr next;

};

Is this how I should write functions taking unique_ptr arguments?

And do I need to use std::move in the calling code?

Base::UPtr b1;
Base::UPtr b2(new Base());

b1->setNext(b2); //should I write b1->setNext(std::move(b2)); instead?

Source: (StackOverflow)

How is "int main(){(([](){})());}" valid C++?

I recently came across the following esoteric piece of code.

int main(){(([](){})());}

Reformat it as follows to make it more readable:

int main(){
    (([](){})());   //  Um... what?!?!
}

But I can't get my head around how (([](){})()) is valid code.

  • It doesn't look like function pointer syntax.
  • It can't be some operator overloading trick. The code compiles as is.

Google didn't help much with this all-symbol search. But it compiles in Visual Studio 2010 and outputs nothing. There were no errors, and no warnings. So it looks like valid code.

I've never seen any valid code that is so bizarre outside of Javascript and C function pointers.

Can someone explain how this is valid C++?


Source: (StackOverflow)

When should I really use noexcept?

The noexcept keyword can be appropriately applied to many function signatures, but I am unsure as to when I should consider using it in practice. Based on what I have read so far, the last-minute addition of noexcept seems to address some important issues that arise when move constructors throw. However, I am still unable to provide satisfactory answers some practical questions that led me to read more about noexcept in the first place.

  1. There are many examples of functions that I know will never throw, but for which the compiler cannot determine so on its own. Should I append noexcept to the function declaration in all such cases?

    Having to think about whether or not I need to append noexcept after every function declaration would greatly reduce programmer productivity (and frankly, would be a pain). For which situations should I be more careful about the use of noexcept, and for which situations can I get away with the implied noexcept(false)?

  2. When can I realistically expect to observe a performance improvement after using noexcept? In particular, give an example of code for which a C++ compiler is able to generate better machine code after the addition of noexcept.

    Personally, I care about noexcept because of the increased freedom provided to the compiler to safely apply certain kinds of optimizations. Do modern compilers take advantage of noexcept in this way? If not, can I expect some of them to do so in the near future?


Source: (StackOverflow)