mercredi 9 juin 2021

Taking address of the temporary in same piece of code - always safe?

I need to test some code, and methods accepts pointers to objects. As I want to write many tests, simplified my tests looks like this

bool addPoint(Point* point);

EXPECT_TRUE(addPoint(&Point(0, 0, 0)));
EXPECT_TRUE(addPoint(&Point(-10, 0, 0)));
EXPECT_TRUE(addPoint(&Point(0, -10, 0)));
//and so on

It works fine. I know that taking address of the temporary variable isn't a good style, but otherwise I will need to type much more, creating proper Point objects and passing them into test.

As far as I know taking address of the temporary isn't UB, so I can rely that my tests will always works as they works now? Or there are some other downsides of taking address of the temporary?

Aucun commentaire:

Enregistrer un commentaire