On 05/ 7/17 05:38 AM, Jorgen Grahn wrote:
> On Sat, 2017-05-06, David Brown wrote:
>> On 05/05/17 22:25, Ian Collins wrote:
>>> On 05/ 5/17 03:00 PM,
woodb...@gmail.com wrote:
>>>> I agree with him ...
https://arne-mertz.de/2017/03/smelly-pair-tuple/
>>>
>>> A poorly written article by someone who doesn't appear to understand
>>> where and how tuples are used.
>>>
>>
>> Or at least, where and how tuples /should/ be used. Maybe he has seen
>> them misused a lot.
>
> At this point, I'd appreciate some words about how they should be
> used. I haven't (as far as I know) had reason to use them myself yet.
I guess there are a couple of glib answers to get out of the way first:
1) whenever you would have used a hand rolled typelist
2) whenever you want to bamboozle your colleagues :)
Regarding the first point: tuples are to compile time operations what
structs are to run time operations. That point was completely
overlooked in the article, especially where the authour criticised the
use of std::get to access members of a tuple. If you don't do much
generic programming with templates, you probably won't use them.
My most frequent use of a tuple is to check a type is valid for a
template. A recent requirement from my work was given a class template
used to hold the latest instance of a particular system message, fail at
compile time if the template get() method was instantiated with a type
that hadn't been created. My solution was to built a tuple of all the
types created and to embed a std::get<T>(Tuple) in the get() member.
Another similar use is for classes that subscribe to a number of system
messages. Rather than have a receive member function I use a template
member function and store the messages in a tuple. If someone tries to
send the wrong message type, the code fails to compile in the same was
it would with individual receive member functions.
--
Ian