jeudi 30 juin 2016

How to combine operator << with the comma operator to assign multiple values to an array or vector class member?

I would like to overload operator << combined with the comma operator in order to provide a way of assigning multiple comma-separated values to a std::array or std::vector class member.

For example, given the following class declaration:

class MyClass{
public:
   A() {}
   ~A() {} 
private:
   std::array<double, 5> a_{};
}

I would like to be able to use the following syntax:

MyClass m;
m << 9, 10, 11, 99, 5;

Which in turn would update a_ by setting the corresponding values (9 at a_[0], 10 at a_[1] and so on). Operator << should also raise an error if the number of comma-separated values does not match std::array::size

Thanks for any advice on how to proceed!

Aucun commentaire:

Enregistrer un commentaire