C++17 In Detail

01 April 2019

C++ April Fool's Quiz

C++ Quiz

It’s April Fool’s Day, so let’s have some fun :) Last year I announced fake news about C++: deprecation of Raw Pointers and two years ago I wanted to have C++18. So what’s coming this time?

A quiz!

Answer the questions, and find two that makes no sense and don’t have any correct answer… can you solve such test?

Updated!: the quiz is no longer accepting answers, see the key below.

The Rules

Two of the questions in the quiz below are “April Fool’s” and don’t make sense, and they don’t have the correct answer.
In that case, you can click “that’s a joke!” answer.

If you leave your email address, you’ll take a chance to win a coupon for C++ ebooks.

Your email address will be only used for this quiz and nothing else; you won’t receive any extra message from me in the future.

If you don’t want to show me the email address, then you can write down the answers and send them to bartlomiej DOT filipek AT bfilipek_DOT_com
please mention the question as they are shuffled.

The quiz lasts for the next 48h (till Wed 3rd April, 8 am PL Time), and I'll contact winners on Friday.

Awards

Your answers will be sorted by the number of points you get (each correct answer is 1 point, the wrong answer is 0), and then I’ll pick the winners:

  • I have 3 coupons 100% off for my book C++17 In Detail
  • 3 100% off coupons for Rainer’s Grimm Concurrency in C++:
  • 5 coupons 60% off for C++17 In Detail

If there are more people with the same point count qualifying for the prize, then I’ll draw the winners randomly.

Notes

The questions come from my recent articles

The Quiz

The quiz is no longer accepting answers...

The quiz with the answers in bold

Questions and the key:

Sometimes std::any doesn’t need to perform extra memory allocation

  • correct - because of Small Buffer Optimisation
  • no - std::any always allocates
  • correct - thanks to type erasure
  • that’s a joke!

What’s the false statement about std::variant?

  • With std::variant you can implement runtime polymorphism for unrelated types
  • std::variant always performs extra memory allocation
  • std::variant might get into invalid state, especially when there’s exception thrown and the old value is already destroyed
  • that’s a joke! all above answers are true for std::variant

(tricky, as we asked for the false statement)

What’s the shortest declaration of a lambda in C++17?

  • [] { }(correct)
  • []() { }
  • [](auto x) { return x; }
  • That’s a joke!

Which sentence is true about the below code?

auto foo = []<typename T>(const std::vector<T>& vec) { 
    return vec.size();
}
  • that’s a template lambda, possible in C++20
  • that’s a generic lambda
  • that’s a joke (none of the answers is true)

What’s the true statement about the following code?

std::vector<int> myVec = ...;
std::sort(std::execution::par, myVec.begin(), myVec.end());
  • Performs sorting in parallel, but the standard requires only execution on CPU and not GPU
  • Can interleave instructions and use vectorisation
  • If the implementation cannot run it in parallel it throws an exception
  • That’s a joke! none of the other options are true

(the code might perform sorting in parallel, but the standard doesn’t specify the hardware requirements)

In C++17, What instructions are allowed in a constexpr function?

  • try and catch
  • goto
  • asm
  • That’s a joke! none of the mentioned instructions are allowed in C++17’s constexpr function

std::any is:

  • it’s a template class, you need to specify at least one of the stored types
  • it’s not a class template
  • it’s a template class, you need to list all possible types that it might store
  • that’s a joke! none of the above answers are true

What’s true about the below code:

template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
  • that’s a possible implementation for the overload pattern, the code compiles in C++17
  • that’s invalid code in C++17
  • the code requires C++20 features: extended -> operator and concepts
  • that’s a joke! none of the above answers are true

In C++17: How to capture a movable-only type? (for example a unique_ptr)

  • use a regular capture [ptr]() { } - lambdas has special support for movable-only types
  • use a capture with initialiser, for example [ptr = std::move(ptr)]() { }
  • use generic lambda
  • that’s a joke! it’s not possible to capture such type using the ideas above

Can you use std::optional<int *> ?

  • yes, std::optional has a special support for pointers and booleans (pointer is removed)
  • yes, you can, but it’s quite ambiguous as int* is also nullable
  • it’s not possible, the code won’t compile
  • it’s a joke, none of the above answers are true

Thanks!

Thanks for taking part in the game! I'm waiting for your answer for the next 48 hours (till Wed 3rd April, 8am PL time)

Get my free ebook about C++17!

More than 50 pages about the new Language Standard.

C++17 in detail, by Bartlomiej Filipek

C++17 In Detail
© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.