Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #87752

Re: How can I get the type in a C++17 fold function?

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject Re: How can I get the type in a C++17 fold function?
Date 2022-12-08 12:18 +0000
Organization Aioe.org NNTP Server
Message-ID <tmskna$7a9$1@gioia.aioe.org> (permalink)
References <tmsbtq$fp6$1@gioia.aioe.org>

Show all headers | View raw


Muttley@dastardlyhq.com wrote:
> eg:
> 
> template<typename... T>
> auto sum(T&&... args)
> {
>         if (typeid(T) == typeid(string)) { do something }
>         return (args + ...);
> }
> 
> However doing typeid on T gives 
> 
> error: expression contains unexpanded parameter pack 'T'
> 
> in clang.
> 
> Is there a way to get the type inside the function?

The problem is that T there is not a type, but a parameter pack.
You can't 'typeid(T)' when T is a parameter pack (what would that
even mean? A parameter pack is essentially a bunch of types).

If you want to iterate through all the types in that parameter
pack and do one thing or another depending on a particular type,
you'll need to do it in the more "traditional" C++11 way of
iterating parameter packs. In other words, something along the
lines of:

void doSomething(const std::string& s) { /* ... */ }

template<typename T>
void doSomething(const T&) {}

template<typename First>
auto sum(First&& first) { return first; }

template<typename First, typename... Rest>
auto sum(First&& first, Rest&&... rest)
{
    doSomething(first);
    return first + sum(std::forward<Rest>(rest)...);
}

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I get the type in a C++17 fold function? Muttley@dastardlyhq.com - 2022-12-08 09:48 +0000
  Re: How can I get the type in a C++17 fold function? Juha Nieminen <nospam@thanks.invalid> - 2022-12-08 12:18 +0000
    Re: How can I get the type in a C++17 fold function? Muttley@dastardlyhq.com - 2022-12-08 15:41 +0000
      Re: How can I get the type in a C++17 fold function? Juha Nieminen <nospam@thanks.invalid> - 2022-12-09 12:57 +0000
  Re: How can I get the type in a C++17 fold function? Öö Tiib <ootiib@hot.ee> - 2022-12-08 04:45 -0800
    Re: How can I get the type in a C++17 fold function? Muttley@dastardlyhq.com - 2022-12-08 15:45 +0000
      Re: How can I get the type in a C++17 fold function? Öö Tiib <ootiib@hot.ee> - 2022-12-08 13:59 -0800
  Re: How can I get the type in a C++17 fold function? Vir Campestris <vir.campestris@invalid.invalid> - 2022-12-14 10:42 +0000
  Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-14 18:02 +0100
    Re: How can I get the type in a C++17 fold function? Muttley@dastardlyhq.com - 2022-12-14 17:17 +0000
      Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-14 18:23 +0100
  Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-15 17:59 +0100
    Re: How can I get the type in a C++17 fold function? Muttley@dastardlyhq.com - 2022-12-15 17:20 +0000
      Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-15 18:37 +0100
    Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-16 05:26 +0100
      Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-16 05:41 +0100
        Re: How can I get the type in a C++17 fold function? scott@slp53.sl.home (Scott Lurndal) - 2022-12-16 15:17 +0000
          Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-16 18:22 +0100
            Re: How can I get the type in a C++17 fold function? scott@slp53.sl.home (Scott Lurndal) - 2022-12-16 17:42 +0000
              Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-16 19:20 +0100
          Re: How can I get the type in a C++17 fold function? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-28 16:41 -0800
            Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-29 03:40 +0100
              Re: How can I get the type in a C++17 fold function? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-28 22:27 -0800
            Re: How can I get the type in a C++17 fold function? Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-29 09:36 +0200
              Re: How can I get the type in a C++17 fold function? Mason <mason.nobody@gmail.com> - 2022-12-29 17:15 +0000
                Re: How can I get the type in a C++17 fold function? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-12-29 19:11 +0100
              Re: How can I get the type in a C++17 fold function? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-29 19:58 -0800
                Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-30 08:09 +0100
              Re: How can I get the type in a C++17 fold function? Michael S <already5chosen@yahoo.com> - 2022-12-30 03:27 -0800
                Re: How can I get the type in a C++17 fold function? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-12-30 14:01 +0100
                Re: How can I get the type in a C++17 fold function? Muttley@dastardlyhq.com - 2022-12-30 15:17 +0000
                Re: How can I get the type in a C++17 fold function? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-12-30 20:04 +0100
                Re: How can I get the type in a C++17 fold function? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-30 13:54 -0800
                Re: How can I get the type in a C++17 fold function? Muttley@dastardlyhq.com - 2022-12-31 15:33 +0000
                Re: How can I get the type in a C++17 fold function? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2022-12-30 17:31 +0000
                Re: How can I get the type in a C++17 fold function? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-01-02 13:30 -0800
                Re: How can I get the type in a C++17 fold function? Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-30 18:36 +0100

csiph-web