basics

Don’t pass lambdas (or other multi-line entities) as parameters to macros--Raymond Chen

Not helping you.

Don’t pass lambdas (or other multi-line entities) as parameters to macros

by Raymond Chen

From the article:

Consider this macro:

#ifdef DEBUG
#define LOG(value) LogValue(value)
#else
// In production, evaluate but don't log.
#define LOG(value) (value)
#endif

This seems not entirely unreasonable, but bad things happen if you pass a multi-line entity as the macro parameter...

Quick Q: How to use auto keyword to assign a variable of type uint32_t or uint64_t in C++

Quick A: Write the type!

Recently on SO:

How to use auto keyword to assign a variable of type uint32_t or uint64_t in C++

I'm assuming you're working with the AAA style suggested by Herb Sutter.

In that case, a nice solution is to simply write:

auto variable_name = uint64_t{ 5000000000 };

This is clear, consistent, and explicitly typed with no nasty C-preprocessor necessary.

How to Design Function Parameters That Make Interfaces Easier to Use (2/3)--Jonathan Boccara

Agree with the logic?

How to Design Function Parameters That Make Interfaces Easier to Use (2/3)

by Jonathan Boccara

From the article:

Let’s continue exploring how to design function parameters that help make both interfaces and their calling code more expressive.

If you missed on the previous episode of this topic, here is what this series of articles contains:

  • Part 1: interface-level parameters, one-parameter functions, const parameters,
  • Part 2: calling contexts, strong types, parameters order,
  • Part 3: packing parameters, processes, levels of abstraction.

Quick Q: What does it mean to return a reference?

Quick A: The returned variable can be modified.

Recently on SO:

What does it mean to return a reference?

It means you return by reference, which is, at least in this case, probably not desired. It basically means the returned value is an alias to whatever you returned from the function. Unless it's a persistent object it's illegal.

For example:

int& foo () {
    static int x = 0;
    return x;
}

//...
int main()
{
    foo() = 2;
    cout << foo();
}

would be legal and print out 2, because foo() = 2 modifies the actual value returned by foo.

However:

int& doit () {
    int x = 0;
    return x;
}

would be illegal (well, accessing the returned value would), because x is destroyed when the method exits, so you'd be left with a dangling reference.

Returning by reference isn't common for free functions, but it is for methods returning members. For example, in the std, the operator [] for common containers return by reference. For example, accessing a vector's elements with [i] returns an actual reference to that element, so v[i] = x actually changes that element.

Also, I hope that "is essentially equal to this code" means that they're semantically sort of (but not really) similar. Nothing more.

How to Retrieve the Firsts from a Collection of Pairs

Simple.

How to Retrieve the Firsts from a Collection of Pairs

by Jonathan Boccara

From the article:

When using the STL, there is a use case that pops up every now and then and that often causes more trouble than necessary: if we have a collection of pairs, like an std::vector of pairs, or simply a std::map, how can we retrieve the collection of the first elements of each item in the collection?