Articles & Books

Interview with Scott Meyers at Yandex -- Coder_CPP

In case you missed the transcript a few days ago on Reddit:

Interview with Scott Meyers at Yandex

by Coder_CPP

From the interview:

... So I don’t think that in ten years everyone will be using managed languages. I think that C++ is going to remain an extremely important language for production use. Simply because if your goal is to get the maximum of performance out of the machine. If that is what is most important to you. I think that even these days the primary competitors are C and C++. Those are pretty much your choices. And C++ is just a much more expressive language. I have to say that I’m not a C programmer. I've never really been a C programmer. But I think that just a simple notion of destructors is so unbelievably powerful, that as a C programmer I would consider moving to C++ just so I could compile and get destructors. And, obviously, there is a lot more? to it than that...

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.

Semaphores are Surprisingly Versatile --Jeff Preshing

An interesting approach on how semaphores could be used in Modern C++ multithreaded programming:

Semaphores are Surprisingly Versatile

by Jeff Preshing

From the article:

In multithreaded programming, it’s important to make threads wait. They must wait for exclusive access to a resource. They must wait when there’s no work available. One way to make threads wait – and put them to sleep inside the kernel, so that they no longer take any CPU time – is with a semaphore.

I used to think semaphores were strange and old-fashioned. They were invented by Edsger Dijkstra back in the early 1960s, before anyone had done much multithreaded programming, or much programming at all, for that matter. I knew that a semaphore could keep track of available units of a resource, or function as a clunky kind of mutex, but that seemed to be about it.

My opinion changed once I realized that, using only semaphores and atomic operations, it’s possible to implement all of the following primitives:

  1. A Lightweight Mutex
  2. A Lightweight Auto-Reset Event Object
  3. A Lightweight Read-Write Lock
  4. Another Solution to the Dining Philosophers Problem
  5. A Lightweight Semaphore With Partial Spinning ...

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?

Quick Q: Calling C++ Application Code from C library?

Quick A: You need to create a stub.

Recently on SO:

Calling C++ Application Code from C library?

Folks, Assuming I have a c++ application / library running which implements say

/* Alloc API */
void* my_alloc(int size) {
    return malloc(sizeof(size));
}

This is not under "extern c".

I have a C dynamic library from which I need to call my_alloc, can I directly call that API?

Like,

int test_my_alloc (int size) {
    int *x;

    x = (int*)my_alloc(size);
    if (x == NULL) {
        return 0;
    } else {
        return 1;
    }
}

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?