Short answer: no.
The colon is not a valid C++ operator, so it cannot be overloaded. And if it could, it still would not be possible to achiev what you need easily, because it most surely would have precedence over the comma operator, which would make your expression be in the lines of A((1:3),(2:10))
, and you are only allowed to overload operators if one of the operands is a user defined type (which is not the case here).
So even with any other operator in place, you could not do anything that looks like this.
What you can do: overload operator()
for your matrix class, for sensible arguments. This could enable you to write something like B = A(1,3,2,10);
, if you define an operator()(int,int, int, int);
My preferred solution would be an operator()
taking either two initializer_list
s in C++11 or two std::array<int,2>
. The former would have to check that the list contains exactly two elements, the latter would need an awkward double brace for initialization - that might disappear in C++14 or later (N3526, but afaik it's not in the CD for C++14). Third possibility would of course be a named class that you could call, presenting a range:
class Matrix {
/* ... */
public:
Matrix operator()(std::initializer_list<int> range1, std::initializer_list<int> range2);
//or:
Matrix operator()(std::array<int,2> range1, std::array<int,2> range2);
//or:
Matrix operator()(Range range1, Range range2);
};
int main() {
Matrix A;
/* ... */
Matrix B = A({1,3}, {2,10}); //beware of A({1,3,2,4,5}, {0}) !
//or:
Matrix B = A({{1,3}}, {{2,10}}); //uhgs...
//or:
Matrix B = A(Range(1,3), Range(2,10)); //more verbose, but also more understandable
}