Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Smelly std::pair and std::tuple

61 views
Skip to first unread message

woodb...@gmail.com

unread,
May 4, 2017, 11:00:53 PM5/4/17
to
I agree with him ... https://arne-mertz.de/2017/03/smelly-pair-tuple/

Std::any doesn't smell good either.


Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

Ian Collins

unread,
May 5, 2017, 4:25:42 PM5/5/17
to
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.

--
Ian

Chris Vine

unread,
May 6, 2017, 11:16:42 AM5/6/17
to
Not just tuples, but the author appears unknowledgeable about the proper
use of bare data structures generally, which are fine for what they are
good at.

I am not greatly surprised that Brian agrees with him.

David Brown

unread,
May 6, 2017, 1:00:31 PM5/6/17
to
Or at least, where and how tuples /should/ be used. Maybe he has seen
them misused a lot.

Jorgen Grahn

unread,
May 6, 2017, 1:38:17 PM5/6/17
to
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.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Daniel

unread,
May 6, 2017, 1:38:57 PM5/6/17
to
On the other hand, the author is right about "I’d ... like to have map<K,V>::value_type be something else than a pair<const K, V>". pair<const K, V> pretty much rules out a broad set of containers such as B Trees and B-Plus trees from satisfying the AssociativeContainer concept.

Daniel

Ian Collins

unread,
May 7, 2017, 1:52:45 AM5/7/17
to
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

Chris Vine

unread,
May 7, 2017, 6:09:29 AM5/7/17
to
To add to this, I have used tuples most frequently to return multiple
values from a function. It is usually much easier than returning a
hand-rolled struct, and you can destructure the return values with
std::tie. I believe C++17 also has a more versatile destructuring bind
for tuples for this explicit purpose, although I have not used it.

Another thing I have sometimes used them for is compile-time function
overloading - you pass a variable number of arguments as a single
(run-time) object, the tuple, which is introspectable at compile time.
I have mainly used it for type erasure, say for constructing opaque
callback objects, but I suspect it is something which has wider uses,
such as the ones you have mentioned.

And sometimes they are just more convenient than passing hand-rolled
structs as an argument, just as std::pair can be more useful than
providing a number of hand-rolled two element structs in some cases.

Chris
0 new messages