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


Groups > comp.lang.c++ > #87750 > unrolled thread

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

Started byMuttley@dastardlyhq.com
First post2022-12-08 09:48 +0000
Last post2022-12-30 18:36 +0100
Articles 20 on this page of 37 — 13 participants

Back to article view | Back to comp.lang.c++


Contents

  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

Page 1 of 2  [1] 2  Next page →


#87750 — How can I get the type in a C++17 fold function?

FromMuttley@dastardlyhq.com
Date2022-12-08 09:48 +0000
SubjectHow can I get the type in a C++17 fold function?
Message-ID<tmsbtq$fp6$1@gioia.aioe.org>
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?

[toc] | [next] | [standalone]


#87752

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-12-08 12:18 +0000
Message-ID<tmskna$7a9$1@gioia.aioe.org>
In reply to#87750
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)...);
}

[toc] | [prev] | [next] | [standalone]


#87757

FromMuttley@dastardlyhq.com
Date2022-12-08 15:41 +0000
Message-ID<tmt0j0$1bnn$1@gioia.aioe.org>
In reply to#87752
On Thu, 8 Dec 2022 12:18:52 -0000 (UTC)
Juha Nieminen <nospam@thanks.invalid> wrote:
>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)...);
>}

Thats just variadic templates with a slightly different syntax. AFAIK the
point of folds is not having to manually iterate the arguments yourself as
with 2011 which is little better than C's varargs (and probably less efficient
due to recursion vs pointer incrementing).

[toc] | [prev] | [next] | [standalone]


#87776

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-12-09 12:57 +0000
Message-ID<tmvbci$1n54$2@gioia.aioe.org>
In reply to#87757
Muttley@dastardlyhq.com wrote:
> On Thu, 8 Dec 2022 12:18:52 -0000 (UTC)
> Juha Nieminen <nospam@thanks.invalid> wrote:
>>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)...);
>>}
> 
> Thats just variadic templates with a slightly different syntax. AFAIK the
> point of folds is not having to manually iterate the arguments yourself as
> with 2011 which is little better than C's varargs (and probably less efficient
> due to recursion vs pointer incrementing).

Well, if you can come up with a fold expression that does what you want,
go right ahead.

[toc] | [prev] | [next] | [standalone]


#87754

FromÖö Tiib <ootiib@hot.ee>
Date2022-12-08 04:45 -0800
Message-ID<c0e8be5f-5276-4de7-9581-1a91982a4501n@googlegroups.com>
In reply to#87750
On Thursday, 8 December 2022 at 11:49:01 UTC+2, Mut...@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?

There are too lot of ways but I usually check the types in enable_if 
as I've used to see that and all compilers can already process that.
If needed I make checking templates myself.

#include <iostream>
#include <string>

// make checking for char array type
template <class> struct is_bounded_char_array : std::false_type {};
template <size_t N> struct is_bounded_char_array<char[N]> : std::true_type {};

// if not std::string, char* and char array 
template<typename T, 
 std::enable_if_t<
!std::is_same_v<std::string, T> 
 && !std::is_same_v<char*, T>
 && !is_bounded_char_array<T>{}
 , bool> = true>
// return whatever it is without processing
auto process_value(T const& v) {return v;}

// for things disabled above return result of string to double conversion
// or whatever your "do something" means
double process_value(std::string const& s) { return std::stod(s); } 

// now your sum is simple to write
template<typename... T>
auto sum(T&&... args) {return (process_value(args) + ...); }

// and demo should output 10
int main()
{
    char arr[] = "1";
    char* ptr = arr;
    std::cout << sum(ptr, 2, "3.0", std::string("4")) << "\n";
}

[toc] | [prev] | [next] | [standalone]


#87758

FromMuttley@dastardlyhq.com
Date2022-12-08 15:45 +0000
Message-ID<tmt0rd$1fku$1@gioia.aioe.org>
In reply to#87754
On Thu, 8 Dec 2022 04:45:54 -0800 (PST)
=?UTF-8?B?w5bDtiBUaWli?= <ootiib@hot.ee> wrote:
>On Thursday, 8 December 2022 at 11:49:01 UTC+2, Mut...@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?
>
>There are too lot of ways but I usually check the types in enable_if 
>as I've used to see that and all compilers can already process that.
>If needed I make checking templates myself.
>
>#include <iostream>
>#include <string>
>
>// make checking for char array type
>template <class> struct is_bounded_char_array : std::false_type {};
>template <size_t N> struct is_bounded_char_array<char[N]> : std::true_type {};
>
>// if not std::string, char* and char array 
>template<typename T, 
> std::enable_if_t<
>!std::is_same_v<std::string, T> 
> && !std::is_same_v<char*, T>
> && !is_bounded_char_array<T>{}
> , bool> = true>
>// return whatever it is without processing
>auto process_value(T const& v) {return v;}

Clearly you know more about the dustier corners of C++ than me. enable_if is 
the only one of those tests I've heard of.

[toc] | [prev] | [next] | [standalone]


#87766

FromÖö Tiib <ootiib@hot.ee>
Date2022-12-08 13:59 -0800
Message-ID<605dfc45-39d5-4abf-bc7e-a30c83a62e00n@googlegroups.com>
In reply to#87758
On Thursday, 8 December 2022 at 17:46:07 UTC+2, Mut...@dastardlyhq.com wrote:
> On Thu, 8 Dec 2022 04:45:54 -0800 (PST) 
> =?UTF-8?B?w5bDtiBUaWli?= <oot...@hot.ee> wrote: 
> >On Thursday, 8 December 2022 at 11:49:01 UTC+2, Mut...@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? 
> > 
> >There are too lot of ways but I usually check the types in enable_if 
> >as I've used to see that and all compilers can already process that. 
> >If needed I make checking templates myself. 
> > 
> >#include <iostream> 
> >#include <string> 
> > 
> >// make checking for char array type 
> >template <class> struct is_bounded_char_array : std::false_type {}; 
> >template <size_t N> struct is_bounded_char_array<char[N]> : std::true_type {}; 
> > 
> >// if not std::string, char* and char array 
> >template<typename T, 
> > std::enable_if_t< 
> >!std::is_same_v<std::string, T> 
> > && !std::is_same_v<char*, T> 
> > && !is_bounded_char_array<T>{} 
> > , bool> = true> 
> >// return whatever it is without processing 
> >auto process_value(T const& v) {return v;}
> 
> Clearly you know more about the dustier corners of C++ than me. enable_if is 
> the only one of those tests I've heard of.

Oh there are quite lot of those checks in C++ metaprogramming.
<https://en.cppreference.com/w/cpp/meta> is good reference of those.
I often do not remember what standard was one or other added but
some important things are still built with older compilers so then
I look from there. 

It is good site but tricky to support them ... as I don't need
much geeky merchantise where their "support" link leads.:D


[toc] | [prev] | [next] | [standalone]


#87897

FromVir Campestris <vir.campestris@invalid.invalid>
Date2022-12-14 10:42 +0000
Message-ID<tnc9av$2pa7m$1@dont-email.me>
In reply to#87750
On 08/12/2022 09:48, 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?
> 
Surely the right way to code this is with an explicit specialisation

template<> auto sum(string&& args)

rather than a runtime check?

Andy

[toc] | [prev] | [next] | [standalone]


#87918

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-14 18:02 +0100
Message-ID<tncvia$2r3r6$1@dont-email.me>
In reply to#87750
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 makes the check for each variable:

#include <iostream>
#include <type_traits>

using namespace std;

int main()
{
	auto variadic = []<typename ... Args>( Args &&... args )
	{
		auto onString = []<typename T>( T &&t )
		{
			if constexpr( is_same_v<remove_cvref_t<T>, string> )
				cout << t << endl;
		};
		(onString( forward<Args>( args ) ), ...);
	};
	variadic( string( "hello world"), 123 );
}

[toc] | [prev] | [next] | [standalone]


#87921

FromMuttley@dastardlyhq.com
Date2022-12-14 17:17 +0000
Message-ID<tnd0eo$1pag$1@gioia.aioe.org>
In reply to#87918
On Wed, 14 Dec 2022 18:02:26 +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 makes the check for each variable:
>
>#include <iostream>
>#include <type_traits>
>
>using namespace std;
>
>int main()
>{
>	auto variadic = []<typename ... Args>( Args &&... args )
>	{
>		auto onString = []<typename T>( T &&t )
>		{
>			if constexpr( is_same_v<remove_cvref_t<T>, string> )
>				cout << t << endl;
>		};
>		(onString( forward<Args>( args ) ), ...);
>	};
>	variadic( string( "hello world"), 123 );
>}

Arrrgh!

[Runs screaming to the hills...]

[toc] | [prev] | [next] | [standalone]


#87922

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-14 18:23 +0100
Message-ID<tnd0p4$2r87q$1@dont-email.me>
In reply to#87921
Am 14.12.2022 um 18:17 schrieb Muttley@dastardlyhq.com:
> On Wed, 14 Dec 2022 18:02:26 +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 makes the check for each variable:
>>
>> #include <iostream>
>> #include <type_traits>
>>
>> using namespace std;
>>
>> int main()
>> {
>> 	auto variadic = []<typename ... Args>( Args &&... args )
>> 	{
>> 		auto onString = []<typename T>( T &&t )
>> 		{
>> 			if constexpr( is_same_v<remove_cvref_t<T>, string> )
>> 				cout << t << endl;
>> 		};
>> 		(onString( forward<Args>( args ) ), ...);
>> 	};
>> 	variadic( string( "hello world"), 123 );
>> }
> 
> Arrrgh!
> 
> [Runs screaming to the hills...]
> 

If you know the lanuguage this is easy to read.

[toc] | [prev] | [next] | [standalone]


#87961

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-15 17:59 +0100
Message-ID<tnfjnt$34jgb$1@dont-email.me>
In reply to#87750
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 );
}

[toc] | [prev] | [next] | [standalone]


#87962

FromMuttley@dastardlyhq.com
Date2022-12-15 17:20 +0000
Message-ID<tnfl11$1v3u$1@gioia.aioe.org>
In reply to#87961
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.

[toc] | [prev] | [next] | [standalone]


#87963

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-15 18:37 +0100
Message-ID<tnfm0l$34p9k$1@dont-email.me>
In reply to#87962
Am 15.12.2022 um 18:20 schrieb Muttley@dastardlyhq.com:

> 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.

This is just an example. There's almost never a need to get the type of
a variadic argument by index.

[toc] | [prev] | [next] | [standalone]


#87970

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-16 05:26 +0100
Message-ID<tngs1d$3adp3$1@dont-email.me>
In reply to#87961
Now I found a really simple solution:

#include <iostream>
#include <concepts>
#include <type_traits>

using namespace std;

int main()
{
	auto variadic = []<typename ... Args>( Args &&... args )
	{
		auto tupl = make_tuple( ref( args ) ... );
		if constexpr( requires( decltype(tupl) tpl ) { { get<0>( tpl ) }; } )
		{
			auto &firstElem = get<0>( tupl );
			if constexpr( is_same_v<remove_cvref_t<decltype(firstElem)>, string> )
				cout << firstElem << endl;
		}
	};
	variadic( string( "hello world"), 123 );
}

[toc] | [prev] | [next] | [standalone]


#87971

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-16 05:41 +0100
Message-ID<tngss9$3af0c$1@dont-email.me>
In reply to#87970
Now it's so simple that anyone should understand this:

#include <iostream>
#include <concepts>
#include <type_traits>

using namespace std;

int main()
{
	auto variadic = []<typename ... Args>( Args &&... args )
	{
		if constexpr( sizeof ...(Args) >= 1 )
		{
			auto tupl = make_tuple( ref( args ) ... );
			auto &firstElem = get<0>( tupl );
			if constexpr( is_same_v<remove_cvref_t<decltype(firstElem)>, string> )
				cout << firstElem << endl;
		}
	};
	variadic( string( "hello world"), 123 );
}

[toc] | [prev] | [next] | [standalone]


#87978

Fromscott@slp53.sl.home (Scott Lurndal)
Date2022-12-16 15:17 +0000
Message-ID<YZ%mL.9038$5S78.5694@fx48.iad>
In reply to#87971
Bonita Montero <Bonita.Montero@gmail.com> writes:
>Now it's so simple that anyone should understand this:
>
>#include <iostream>
>#include <concepts>
>#include <type_traits>
>
>using namespace std;
>
>int main()
>{
>	auto variadic = []<typename ... Args>( Args &&... args )
>	{
>		if constexpr( sizeof ...(Args) >= 1 )
>		{
>			auto tupl = make_tuple( ref( args ) ... );
>			auto &firstElem = get<0>( tupl );
>			if constexpr( is_same_v<remove_cvref_t<decltype(firstElem)>, string> )
>				cout << firstElem << endl;
>		}
>	};
>	variadic( string( "hello world"), 123 );
>}

int main()
{   printf("hello world\n");
}

Is far simpler.  Complexity is evil.


   "While complexity seeks order through addition, simplicity
    seeks it through subtraction. Most people have a built-in bias
    toward addition instead of subtraction. For some reason, the
    concept of more comes naturally to us."

[toc] | [prev] | [next] | [standalone]


#87981

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-16 18:22 +0100
Message-ID<tni9ge$3dodg$1@dont-email.me>
In reply to#87978
Am 16.12.2022 um 16:17 schrieb Scott Lurndal:
> Bonita Montero <Bonita.Montero@gmail.com> writes:
>> Now it's so simple that anyone should understand this:
>>
>> #include <iostream>
>> #include <concepts>
>> #include <type_traits>
>>
>> using namespace std;
>>
>> int main()
>> {
>> 	auto variadic = []<typename ... Args>( Args &&... args )
>> 	{
>> 		if constexpr( sizeof ...(Args) >= 1 )
>> 		{
>> 			auto tupl = make_tuple( ref( args ) ... );
>> 			auto &firstElem = get<0>( tupl );
>> 			if constexpr( is_same_v<remove_cvref_t<decltype(firstElem)>, string> )
>> 				cout << firstElem << endl;
>> 		}
>> 	};
>> 	variadic( string( "hello world"), 123 );
>> }
> 
> int main()
> {   printf("hello world\n");
> }
> 
> Is far simpler.  Complexity is evil.

It wasn't the job to print out hello world. This was just to show
that what I was actually trying to achieve is working. Show me a
simpler solution to check if the first variadic parameter exists
and it is a string-object.

[toc] | [prev] | [next] | [standalone]


#87982

Fromscott@slp53.sl.home (Scott Lurndal)
Date2022-12-16 17:42 +0000
Message-ID<m62nL.82593$gGD7.47295@fx11.iad>
In reply to#87981
Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 16.12.2022 um 16:17 schrieb Scott Lurndal:
>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>> Now it's so simple that anyone should understand this:
>>>
>>> #include <iostream>
>>> #include <concepts>
>>> #include <type_traits>
>>>
>>> using namespace std;
>>>
>>> int main()
>>> {
>>> 	auto variadic = []<typename ... Args>( Args &&... args )
>>> 	{
>>> 		if constexpr( sizeof ...(Args) >= 1 )
>>> 		{
>>> 			auto tupl = make_tuple( ref( args ) ... );
>>> 			auto &firstElem = get<0>( tupl );
>>> 			if constexpr( is_same_v<remove_cvref_t<decltype(firstElem)>, string> )
>>> 				cout << firstElem << endl;
>>> 		}
>>> 	};
>>> 	variadic( string( "hello world"), 123 );
>>> }
>> 
>> int main()
>> {   printf("hello world\n");
>> }
>> 
>> Is far simpler.  Complexity is evil.
>
>It wasn't the job to print out hello world. This was just to show
>that what I was actually trying to achieve is working. Show me a
>simpler solution to check if the first variadic parameter exists
>and it is a string-object.
>

Why?  Of what usefulness would it be in real-world code?  It
remains unnecessary complexity.

[toc] | [prev] | [next] | [standalone]


#87983

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-12-16 19:20 +0100
Message-ID<tnict7$3e1rh$1@dont-email.me>
In reply to#87982
Am 16.12.2022 um 18:42 schrieb Scott Lurndal:

> Why?  Of what usefulness would it be in real-world code?

I didn't ask for that.

> It remains unnecessary complexity.

If you have this problem, then I think this is the simplest solution.
People on Stack Overflow don't have any issues with that, but here
the people act like rebels.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c++


csiph-web