2

I saw this example of the Eigen library :

#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
  Matrix2d a;
  a << 1, 2,
       3, 4;
  MatrixXd b(2,2);
  b << 2, 3,
       1, 4;
  std::cout << "a + b =\n" << a + b << std::endl;
  std::cout << "a - b =\n" << a - b << std::endl;
  std::cout << "Doing a += b;" << std::endl;
  a += b;
  std::cout << "Now a =\n" << a << std::endl;
  Vector3d v(1,2,3);
  Vector3d w(1,0,0);
  std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
}

And this part is puzzling me :

a << 1, 2,
     3, 4;

How can we overload << operator to send multiple parameters like in the example ?

PS : I've already seen this question C++ overloading << operator multiple parameters

Aminos
  • 754
  • 1
  • 20
  • 40
  • 1
    I assume it overloads the comma operator. Eek! –  Jan 13 '19 at 20:55
  • 1
    An overloaded comma operator applies? – πάντα ῥεῖ Jan 13 '19 at 20:55
  • Which part of the answers on that page didn't you get? – Lightness Races in Orbit Jan 13 '19 at 21:01
  • 3
    @LightnessRacesinOrbit On that page they explain this cannot be done, and here the OP is showing something they found that does it. – GSerg Jan 13 '19 at 21:05
  • 2
    I believe that would be https://github.com/eigenteam/eigen-git-mirror/blob/master/Eigen/src/Core/CommaInitializer.h. – GSerg Jan 13 '19 at 21:18
  • I didn't know that the comma operator can be overloaded... (the dot one cannot isn't it ?) – Aminos Jan 13 '19 at 21:26
  • 1
    @Aminos Almost any operators can be overloaded in c++. – πάντα ῥεῖ Jan 13 '19 at 21:27
  • what's a definition of operator ? the sign "?" can be overloaded for example ? and no, not all the operators can be overloaded, the "::" cannot, I read it somewhere. – Aminos Jan 13 '19 at 21:30
  • @GSerg: Huh? Rici's answer gives two ways to do it, including a proxy type, which is the way Eigen is doing it. – Lightness Races in Orbit Jan 13 '19 at 22:08
  • @LightnessRacesinOrbit But Rici then says *You can then call that as `anyvar << arg1 << arg2`* while what the OP wanted was `anyvar << arg1, arg2`, and regarding that Rici says *No, that is not possible*. – GSerg Jan 13 '19 at 22:19
  • @GSerg You're only reading the first line of Rici's answer. You have to read the rest of it as well. – Lightness Races in Orbit Jan 13 '19 at 22:25
  • 1
    @LightnessRacesinOrbit I really can't see what I'm missing. The call of the form `anyvar << arg1, arg2` is only mentioned in the Rici's answer as "No, that is not possible", and the need to overload the comma operator is not mentioned in the answer either, which would be a requirement for making `anyvar << arg1, arg2` possible. Things that are mentioned as possible are `anyvar << std::make_pair(a1, a2)` and `anyvar << arg1 << arg2`. – GSerg Jan 13 '19 at 22:38
  • @GSerg: You can implement the proxy class approach with any operator you like, including `operator,`... The phrase "no, that is not possible" answers the question "if we can give two or more parameters to an overload of operator <<", to which it is the correct answer. – Lightness Races in Orbit Jan 13 '19 at 22:39

0 Answers0