C++ Riddle - guest appearance (always welcomed) - April-11, 2022

After enjoying @amir’s C++ riddles, I thought to also give it a try. So let me know how much you hate it :wink:
You are given following C++20 code:

//TODO: A
//TODO: B
//TODO: C

template<A Almost>
void f() {}

template<B Always>
void g() {}

template<C Auto>
void h() {}

int main() {
    f<B>();
    g<B{}>();
    h<C<>{}>();
}

Implement A, B and C such that this program compiles correctly.

1 Like
template<class T>
concept A = std::default_initializable<T>;

struct B {};

template<class T=void>
struct C {};

Great answer @avilachmish , that’s correct! The motivation for this riddle comes from the blog post Ambiguity in template parameters from Barry Revzin. He mentions that before C++20, when you look through unfamiliar code and come accross template <Kind Name> where you didn’t know what Kind was, you could rightly conclude that this is a non-type template parameter (most likely some kind of enumeration). But since C++20 Kind could be:

  • a concept (A)
  • a literal class type (B)
  • or even a class template (C)

I hope you could enjoy that riddle.
Cheers,
Kilian

2 Likes