C ++ operator overloads how to implement a template like ctor Mat_ <type> in opencv

In opencv, I can build a Mat object like this:

Mat mat = (Mat_<int>(2, 3) << 1, 2, 3, 4, 5, 6);

      

So it is convenient to initialize an instance Mat_<type>

, and if I have my own simplified matrix class Mat2D

in which I will use this template, but how to do it?
Update: I tried using a variable length parameter list, but error C2829: 'operator <<' cannot have a variable parameter list

.

+1


source to share


2 answers


WARNING: DO NOT DO THIS. YOU HAVE BEEN WARNED.

You can achieve this using operator overloading, but this is a very bad idea, as I will explain later.

I am assuming the class Mat

has a constructor that takes Mat_<int>

.

I am assuming that the template template Mat_<T>

has a method Insert()

that knows how to insert one element into the matrix. I'll let you figure it out, but for that you need to know where to insert it.

It's easy to overload with this method operator<<

:

template<typename T>
Mat_<T>& operator<<(Mat_<T>& mat, const T& el)
{
  mat.Insert(el);
  return mat;
}

      

And we can overload operator,

to call this overloaded operator<<

:



template<typename T>
Mat_<T>& operator,(Mat_<T>& mat, const T& el)
{
  return mat << el;
}

      

Everything works fine and you can use your own syntax. Now I will explain why this is a bad idea.

Overloading operator<<

in this way is perfectly reasonable. This is an input operator and our overload is inserting an element into the matrix. This is what people would expect; so far so good.

But overloading operator,

is not. The meaning of this operator is to "evaluate two expressions, then return the last"; this is clearly not what our overloaded operator does. Optional users will try to use the operator in a ,

standard way (for example, in a loop for

) and will not understand why their code is not working. You shouldn't overload an operator to perform a custom operation unless you want to be hated by the person using your code; probably sometime later.

In fact, although the standard allows overloading operator,

, you probably should never do this, because it is impossible to write code that does a standard operation. You may consider this to be a bug in the standard that is maintained for backward compatibility.

And, if you are considering overloading operator,

to take two int

and somehow combine them together, not only are there even more serious drawbacks: it is not allowed to overload an operator when all operands are built-in types.

So, in short: you can do it, but it's a bad idea and will throw bugs in unexpected places in your code.

+3


source


It is a combination of a fluid builder pattern using the input and comma operators as a simple DSL. I don't see any dangers (if done correctly) as your DSL defines a specific context in which these operators apply. Take a look at boost :: assign if you're looking for inspiration. Anyway, I use DSL word elements instead of operators, but that's just a matter of my personal preference ...



+1


source







All Articles