Meeting 23 May 2019

Quirks in Class Template Argument Deduction

Barry Revzin: https://brevzin.github.io/c++/2018/09/01/quirks-ctad/

std::tuple<int> foo();

std::tuple x = foo(); // tuple<tuple<int>>?
auto y = foo();       // tuple<int>

What is the intent behind the declaration of variable x? Are we constructing a new thing (the CTAD goal) or are we using std::tuple as annotation to ensure that x is in fact a tuple (the Concepts goal)?

A clearer example:

// The tuple case
// unquestionably, tuple<int>
std::tuple a(1);

// unquestionably, tuple<tuple<int>,tuple<int>>
std::tuple b(a, a);

// ??
std::tuple c(a);

clamp_cast -- A saturating arithmetic cast

https://github.com/p-groarke/clamp_cast

A narrowing cast that does the right thing. clamp_cast will saturate output values at min or max if the input value would overflow / underflow.

double ld = -42.0;
unsigned char uc = clamp_cast<unsigned char>(ld);
// uc == 0

float f = 500000.f;
char c = clamp_cast<char>(f);
// c == 127

Same function parameters with different return type in C++17/C++20

https://www.reddit.com/r/cpp/comments/aoidsi/what_is_the_solution_for_same_function_parameters/

Before:

template<typename R>
R foo(int i)
{ ... }

foo<string>(1);

After:

template<class F> struct Auto : F {
    // conversion operator
    template<class T> operator T() {
        return F::template operator()<T>();
    }
};

template<class F> Auto(F) -> Auto<F>; // deduction guide

After:

template<class... A>
auto fooWrapper(A&&... a) {
    return Auto{[&]<class T>() { return foo<T>(std::forward<A>(a)...); }};
};

template<class... A>
auto fooWrapper(int i) {
    return Auto{[=]<class T>() { return foo<T>(i); }};
};

double d = fooWrapper(42);

Data alignment the C++ way

https://vorbrodt.blog/2019/04/06/data-alignment-the-c-way/

Before modern C++:

struct Old
{
    int x;
    char padding[16 - sizeof(int)];
};

Now:

struct alignas(16) New
{
    int x;
};

Nameof operator for modern C++

https://github.com/Neargye/nameof

See also: CTTI https://github.com/Manu343726/ctti

Is Microsoft/GSL still being maintained?

It is used by the brand new Terminal App. That alone is an indication of effort.

Twitter: identifier case

/img/case1.png /img/case2.png /img/case3.png