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


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

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

From Muttley@dastardlyhq.com
Newsgroups comp.lang.c++
Subject Re: How can I get the type in a C++17 fold function?
Date 2022-12-15 17:20 +0000
Organization Aioe.org NNTP Server
Message-ID <tnfl11$1v3u$1@gioia.aioe.org> (permalink)
References <tmsbtq$fp6$1@gioia.aioe.org> <tnfjnt$34jgb$1@dont-email.me>

Show all headers | View raw


On Thu, 15 Dec 2022 17:59:03 +0100
Bonita Montero <Bonita.Montero@gmail.com> wrote:
>Am 08.12.2022 um 10:48 schrieb Muttley@dastardlyhq.com:
>> 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?
>> 
>
>This is exactly what you're looking for.
>The first parameter of the outer lambda call is
>checked if it is a string and if it is it is cout'ed:
>
>#include <iostream>
>#include <concepts>
>#include <type_traits>
>
>using namespace std;
>
>int main()
>{
>	auto variadic = []<typename ... Args>( Args &&... args )
>	{
>		auto unroll = [&]<size_t Index, size_t ... Indices>( 
>integral_constant<size_t, Index>, index_sequence<Indices ...> )
>		{
>			auto onString = []<typename T>( T &&t )
>			{
>				if constexpr( is_same_v<remove_cvref_t<T>, string> )
>					cout << t << endl;
>			};
>			((Indices == Index ? onString( forward<Args>( args ) ) : (void)0), ...);
>		};
>		unroll( integral_constant<size_t, 0>(), make_index_sequence<sizeof 
>....(args)>() );
>	};
>	variadic( string( "hello world"), 123 );
>}

I don't even know what some of those meta functions do. index_sequence,
remove_cvref_t anyone?

It would have been nice if the committee had simply added a way to get the
type of the current argument. Perhaps "typeid(args[0])". They've fucked with
the syntax so much already that overloading [] again wouldn't make much
difference.

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