mercredi 6 avril 2016

Is there a way to use std::array with literals, or initializer lists, for comparison, in the same way you can for assignment? [duplicate]

This question already has an answer here:

Example:

std::array<float, 3> vec;

We can do ...

vec = {1.1, 1.2, 1.3};

Why can't we also do the following, for comparison?

vec == {1.1, 1.2, 1.3}

Instead, it appears that we have to do ...

vec == std::array<float, 3>({1.1, 1.2, 1.3})

... or something similar.

Typedef'ing allows me to do something like ...

typedef std::array<float, 3> vector;
vec == vector({1.1, 1.2, 1.3})

But is there a way to just do ... ?

vec == {1.1, 1.2, 1.3}

Can I overload operator== to accomplish this? It would seem that the compiler should know to interpret {1.1, 1.2, 1.3} as whatever is on the left side of the ==. It does it for =, why not for ==?

Aucun commentaire:

Enregistrer un commentaire