admin管理员组

文章数量:1332345

I am reading the book "C++17 - The Complete Guide", by Nicolai M. Josuttis.

Chapter 14 is entitled "Using variadic using declarations". I don't understand the sample code in it:

// Part 1: "inherit" all function call operators of passed based types:
template<typename... Ts>
struct overload : Ts...
{
    using Ts::operator()...;
}

// Part 2: base types are deduced from passed arguments
// >>>> What is it ?? -> see answer from Ted Lyngmo
// EDIT: That's also based on the fact that overload can use
//       aggregate initialization, and so that overload can be
//       instancied with a constructor with any number and types
//       (but we have to help it to deduce the types).
template<typename... Ts>
overload(Ts...) -> overload<Ts...>;

// Part 3
auto twice = overload {
                [](std::string& s) { s += s; }
                [](auto& v) { v *= 2; }
             };

The code has 3 parts:

  • part 1: I understand we declare a class which will have finally 3 function call operators.

  • part 2: I don't understand this one... - what are we declaring here? Could you explain the syntax, especially overload(Ts...)?

  • part 3: We use aggregate initialization to initialize base classes function call operators.

If I have to read a previous chapter again, let me know which one!

I am reading the book "C++17 - The Complete Guide", by Nicolai M. Josuttis.

Chapter 14 is entitled "Using variadic using declarations". I don't understand the sample code in it:

// Part 1: "inherit" all function call operators of passed based types:
template<typename... Ts>
struct overload : Ts...
{
    using Ts::operator()...;
}

// Part 2: base types are deduced from passed arguments
// >>>> What is it ?? -> see answer from Ted Lyngmo
// EDIT: That's also based on the fact that overload can use
//       aggregate initialization, and so that overload can be
//       instancied with a constructor with any number and types
//       (but we have to help it to deduce the types).
template<typename... Ts>
overload(Ts...) -> overload<Ts...>;

// Part 3
auto twice = overload {
                [](std::string& s) { s += s; }
                [](auto& v) { v *= 2; }
             };

The code has 3 parts:

  • part 1: I understand we declare a class which will have finally 3 function call operators.

  • part 2: I don't understand this one... - what are we declaring here? Could you explain the syntax, especially overload(Ts...)?

  • part 3: We use aggregate initialization to initialize base classes function call operators.

If I have to read a previous chapter again, let me know which one!

Share Improve this question edited Nov 21, 2024 at 9:51 Oodini asked Nov 20, 2024 at 22:01 OodiniOodini 1,3471 gold badge9 silver badges19 bronze badges 2
  • 2 Part 2 is CTAD - Class template argument deduction. – Marek R Commented Nov 20, 2024 at 22:38
  • Well, you gotta expect a few nightmares from Josuttis. – user4581301 Commented Nov 20, 2024 at 22:56
Add a comment  | 

1 Answer 1

Reset to default 6
template<typename... Ts>
overload(Ts...) -> overload<Ts...>;

what are we declaring here ?

This is a user-defined deduction guide. It will be used when the class template overload is instantiated without explicitly specifying any template parameters. It then helps to deduce the class to be overload<Ts...>.

In this context:

auto twice = overload {
                [](std::string& s) { s += s; },
                [](auto& v) { v *= 2; }
             };

... it makes twice of type

overload<decltype(lambda1), decltype(lambda2)>

In C++20, the implicitly generated deduction guides would be enough but in C++17 this deduction guide is needed for this type of instantiation.

本文标签: cNeed help understanding variadic 39using39 declarationsStack Overflow