struct with 2 cells vs std::pair? [duplicate]

In terms of performance: it’s unlikely to change anything, you’re just sugar coating it.

In terms of usability, I would rather use a custom struct, which can be declared this way (by the way):

struct MyElement
{
  int label;
  double value;
};

I am a strong proponent of strong typing, and I much prefer a “real” structure (and better yet, class) than an ad-hoc tuple whenever it’s more than a fleeting thing.

Mainly because:

  • As you noted first and second don’t carry much meaning
  • You cannot add methods / other fields to a std::pair
  • You cannot add class invariants to a std::pair

All in all, I really think that maintenance benefits from using a custom dedicated structure that a one-size-fits-them-all tuple.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)