vendredi 20 novembre 2020

std::tie for less comparison and char arrays

I've always known that to implement in a fast way a operator< for a class, the fastest way that always works is to use std::tie.

E.g.

struct TestInt
{
int a = 0;
int b = 0;
};

TestInt first;
TestInt second(first);

bool aLess = std::tie( first.a, first.b)
  < std::tie(second.a, second.b);
bool bLess = std::tie( first.a, first.b)
  > std::tie(second.a, second.b);
EXPECT_EQ(aLess, false);
EXPECT_EQ(bLess, false);
EXPECT_EQ(aLess, bLess);

The same does not work if you use another struct containing members with array of chars like:

struct TieTestChar
  {
  char a[10];
  int b=0;
  TieTestChar() {strcpy(a, "test");}
  };

TieTestChar first;
TieTestChar second(first);

bool aLess = std::tie( first.a, first.b)
  < std::tie(second.a, second.b);
bool bLess = std::tie( first.a, first.b)
  > std::tie(second.a, second.b);
EXPECT_EQ(aLess, false);
EXPECT_EQ(bLess, false);
EXPECT_EQ(aLess, bLess);

What am I missing?

[test case edited]

Aucun commentaire:

Enregistrer un commentaire