PSA: ADL requires that unqualified lookup has found a function

As seen on the cpplang Slack (hat tip to Jody Hagins). Recall my post “What is the std::swap two-step?” (2020-07-11), where I said:

A qualified call like base::frotz(t) indicates, “I’m sure I know how to frotz whatever this thing may be. No type T will ever know better than me how to frotz.”

An unqualified call using the two-step, like using my::xyzzy; xyzzy(t), indicates, “I know one way to xyzzy whatever this thing may be, but T itself might know a better way. If T has an opinion, you should trust T over me.”

An unqualified call not using the two-step, like plugh(t), indicates, “Not only should you trust T over me, but I myself have no idea how to plugh anything. Type T must come up with a solution; I offer no guidance here.”

Read More

TIL: nullopt_t is not equality-comparable, but monostate is

On Slack, Kilian Henneberger asked for some STL types that are copyable but not equality-comparable. One example is std::function<int()>; see “On function_ref and string_view (2019-05-10). The simplest example is struct S {}; — for compatibility with C, C++98 provided every class type with a copy constructor, but none of them with comparison operators.

Read More

Evirdle

Has it been a week since Birdle already? Wow. Anyway, I’ve made another Wordle variant. I call this one Evirdle, because it is evil. It tries to make your Wordle experience as challenging as possible.

Read More

“Universal reference” or “forwarding reference”?

Two convergent observations from my corner of the C++ world:

1. Multiple book authors pushing the idea that Scott Meyers’ original phrase “universal reference” (for T&&) is actually preferable to the now-Standard term “forwarding reference.”

2. Multiple C++ learners (or perhaps intermediate-level C++ programmers having a senior moment) asking whether there’s an easy way to pass an rvalue expression to a function expecting a const lvalue reference.

Read More

A minimally interesting typo-bug

I just ran into some code like this in a test suite:

struct MoveOnlyWidget {
    MoveOnlyWidget(int);
    MoveOnlyWidget(const MoveOnlyWidget&) = delete;
    MoveOnlyWidget(MoveOnlyWidget&&) = default;
    MoveOnlyWidget& operator=(const MoveOnlyWidget&) = delete;
    MoveOnlyWidget& operator=(MoveOnlyWidget&&) = default;
    MoveOnlyWidget() = default;
};
Read More

volatile means it really happens

During my CppCon 2020 talk “Back to Basics: Concurrency,” someone asked, “Do we ever need to use volatile?” To which I said, “No. Please don’t use volatile for anything ever.” This was perhaps a bit overstated, but at least in the context of basic concurrency it was accurate. To describe the role of volatile in C and C++, I often use the following slogan:

Marking a variable as volatile means that reads and writes to that variable really happen.

If you don’t know what this means, then you shouldn’t use volatile.

Read More

const all the things?

Last week someone posted a /r/cpp thread titled “Declaring all variables local to a function as const”:

Ok, so I’m an old-school C++ programmer using the language now since the early ’90s. I’m a fan of const-correctness for function and member declarations, parameters, and the like. Where, I believe, it actually matters.

Now we have a team member who has jumped on the “everything is const” bandwagon. Every code review includes dozens of lines of local function variables now declared const that litter the review.

Intellectually, I understand the (what I consider mostly insignificant) arguments in favor of this practice, but in nearly 30 years I have never had a bug introduced into my code because a local function variable was mutable when I didn’t expect it. It does nothing for me to aid in code analysis or tracking. It has at most a tiny impact on performance.

Maybe I’m just an old dog finally unable to learn a new trick. So, on this fine Wednesday, who’s up for a religious war? What are y’all doing?

TLDR: I’m not putting const on all the things, either.

Read More

The best engineering interview question I’ve ever gotten, Part 1

It’s been a while since I was on the receiving end of a software engineering interview. But I still remember my favorite interview question. It was at MemSQL circa 2013. (They haven’t even kept their name, so I assume they’re not still relying on this specific interview question. I don’t feel bad for revealing it. It’s a great story that I tell people a lot; I’ve just never blogged it before.)

Read More