basics

CppCon 2014 An Overview of C++11/14, Part II--Leor Zolman

While we wait for CppCon 2015 in September, we’re featuring videos of some of the 100+ talks from CppCon 2014. Here is today’s feature:

An Overview of C++11/14, Part II

by Leor Zolman

(watch on YouTube) (watch on Channel 9)

Summary of the talk:

This accelerated introduction to C++11/14 surveys most of the key additions to the C++ language, including support for increased code clarity (lambdas, uniform initialization, auto, new OOD control) and improved performance (rvalue references, move semantics and perfect forwarding.)

The presentation is designed for those who truly need a quick overview of the new C++, so the focus is on breadth rather than depth. Whenever feasible, new language features are presented in a style showcasing how they improve over their "Old C++" counterparts.

C++ Tail Recursion Using 64-bit variables -- Giovanni Campo

Leveraging the elegance of recursion:

C++ Tail Recursion Using 64-bit variables

by Giovanni Campo

From the article:

Main disadvantage of Recursion in imperative languages is the fact that not always is possible to have tail calls, which means an allocation of the function address (and relative variables, like structs for instance) onto the stack at each call. For deep recursive function this can cause a stack-overflow exception because of a limit to the maximum size of the stack, which is typically less than the size of RAM by quite a few orders of magnitude.

Refactoring my Qt database code

I spend some time on refactoring my database code in Qt:

Refactoring my Qt database code

by Jens Weller

From the article:

For two days I had the chance to clean up my code and do a little refactoring. One of the results is, that my database code now also uses variadic templates. For some time now, I use Qt as the UI and Database frontend of my applications...

Quick Q: Return a local object rvalue reference, right or wrong?

Quick A: Wrong!

Recently on SO:

Return a local object rvalue reference,right or wrong?

I see once return a local object,the compiler will take the return value optimization.(RVO,NRVO).

The part of the Standard blessing the RVO goes on to say that if the conditions for the RVO are met, but compilers choose not to perform copy elision, the object being returned must be treated as an rvalue.


So we just write code like this:

Widget makeWidget()
{
Widget w;
…
return w;//never use std::move(w);
}

I never see somebody write code like this:

Widget&& makeWidget()
{
Widget w;
…
return std::move(w);
}

I know that returns an lvalue reference of local object is always wrong. So, returns an rvalue reference of local object is also wrong?

Secrets of the Conditional (ternary) Operator -- Alex Kulikov

Hot off kukuruku:

Secrets of the Conditional (ternary) Operator

by Alex Kulikov

(Note: Translation of the original Russian-language article here.)

From the article:

Every self-respecting C/C++ programmer knows what the ternary operator is, and most everyone used it at least once in their programs. But do you know all the secrets of the ternary operator? What potential dangers are associated with its use and what features, seemingly not related to its direct purpose, it has? This article gives you the opportunity to test your knowledge and maybe learn something new.

Let's start with a small test...

Quick Q: Why can a constexpr function return a non-constant?

Quick A: Because constexpr means can be evaluated at compile time, not that it will be. And it won’t be if the inputs aren’t compile-time constants.

Recently on SO:

I am confused about a constexpr function?

In C++ Primer, Fifth Edition, §6.5.2:

A constexpr function is defined like any other function but must meet certain restrictions: The return type and the type of each parameter in must be a literal type (§2.4.4, p. 66), and the function body must contain exactly one return statement

but another sentence in this chapter (page 239):

A constexpr function is permitted to return a value that is not a constant

// scale(arg) is a constant expression if arg is a constant expression
constexpr size_t scale(size_t cnt) { return new_sz() * cnt; }

Is it a contradictory summary? I am confused about it.
The return type of scale is literal type?
update: what's the difference between literal type and constant ?

Quick Q: How to write variadic template constructor?

Quick A: use an std::initializer_list.

Recently on SO:

Writting variadic template constructor

Recently I asked this question but now I would like to expand it. I wrote the following class:

template <class T>
class X{
public:
    vector<T> v;
    template <class T>
    X(T n) {
        v.push_back(n);
    }
    template <class T, class... T2>
    X(T n, T2... rest) {
        v.push_back(n);
        X(rest...);
    }
};

When creating and object using

X<int> obj(1, 2, 3);  // obj.v containts only 1

Vector only contains the first value, but not others. I've checked and saw that constructor is called 3 times, so I'm probably creating temp objects and filling their vectors with the rest of the arguments. How do I solve this problem?

Quick Q:How to modify a tuple in a vector of tuples c++?

Quick A: Capture the tuple by reference.

Recently on SO:

Modifying a tuple in a vector of tuples c++

I have a vector of tuples vector<tuple<int,int>> vector; and I want to modify one of the tuples it contains.

for (std::tuple<int, int> tup : std::vector)
{
    if (get<0>(tup) == k)
    {
        /* change get<1>(tup) to a new value
         * and have that change shown in the vector
         */
    }
}

I am unsure how to change the value of the tuple and have the change be reflected in the vector. I have tried using

get<1>(tup) = v;

but that doesn't change the value of the tuple that is in the vector. How can I do this? Thanks.

How to write a standard-like algorithm -- Indi

Explicit C++ has posted a nice tutorial on how to implement an algorithm in C++.

How to write a standard-like algorithm

by Indi

from the article:

Writing a standard-like algorithm should be one of the key parts of a modern C++ beginner’s course outline. This post will be a whirlwind guide through the steps toward creating a standard-like algorithm. The focus is not on the algorithm itself, but on the process of creating one.