I have the following code:
#include <iostream>
#include <array>
typedef std::array<int, 2> eval_t;
eval_t operator*(eval_t e1, eval_t e2) { return {e1[0] * e2[0], e1[1] * e2[1]}; }
int main()
{
eval_t a = {1, 2};
eval_t b = a * {2, 1};
std::cout << "b = (" << b[0] << ',' << b[1] << ')' << std::endl;
}
GCC refused to compile my multiplication:
$ g++ -std=c++11 test.cc
test.cc: In function ‘int main()’:
test.cc:10:17: error: expected primary-expression before ‘{’ token
eval_t b = a * {2, 1};
^
I was naively hoping that the only possible operator*() taking eval_t
as left operand, would be the one I defined, and the right operand would be understood as eval_t
.
Instead, if I write:
eval_t a = {1, 2};
eval_t v = {2, 1};
eval_t b = a * v;
it works.