L
Lionel B
Greetings,
I am trying to implement "element-wise" arithmetic operators for a class
along the following lines (this is a simplified example):
// ----- BEGIN CODE -----
struct X
{
int a,b;
typedef int& optype(int&,int); // ???
template<optype OP> void element_wise(X& x)
{
OP(a,x.a);
OP(b,x.b);
}
X& operator+=(X& x)
{
element_wise< :
perator+=<int> >(x); // ??? (line 15)
return *this;
}
};
int main()
{
X x,y;
x.a = 1;
x.b = 2;
y.a = 3;
y.b = 4;
x += y;
return 0;
}
// ----- END CODE -----
I get the error (gcc):
test.cpp: In member function `X& X:
perator+=(X&)':
test.cpp:15: error: no matching function for call to
`X::element_wise(X&)'
This is perhaps not surprising, in view of that:
// ----- BEGIN CODE -----
int main()
{
int a = 1;
operator += (a,1);
return 0;
}
// ----- END CODE -----
fails to compile with "error: `operator+=' not defined" (this surprised
me somewhat). It seems that function-style prototypes for arithmetic
operators on elementary types don't exist.
So is it at all possible to use arithmetic operators for elementary
types as template arguments? A simple workaround is to wrap the
operators in a function, but it would be neater (and conceivably more
efficient) not to have to do this.
Regards,
I am trying to implement "element-wise" arithmetic operators for a class
along the following lines (this is a simplified example):
// ----- BEGIN CODE -----
struct X
{
int a,b;
typedef int& optype(int&,int); // ???
template<optype OP> void element_wise(X& x)
{
OP(a,x.a);
OP(b,x.b);
}
X& operator+=(X& x)
{
element_wise< :
return *this;
}
};
int main()
{
X x,y;
x.a = 1;
x.b = 2;
y.a = 3;
y.b = 4;
x += y;
return 0;
}
// ----- END CODE -----
I get the error (gcc):
test.cpp: In member function `X& X:
test.cpp:15: error: no matching function for call to
`X::element_wise(X&)'
This is perhaps not surprising, in view of that:
// ----- BEGIN CODE -----
int main()
{
int a = 1;
operator += (a,1);
return 0;
}
// ----- END CODE -----
fails to compile with "error: `operator+=' not defined" (this surprised
me somewhat). It seems that function-style prototypes for arithmetic
operators on elementary types don't exist.
So is it at all possible to use arithmetic operators for elementary
types as template arguments? A simple workaround is to wrap the
operators in a function, but it would be neater (and conceivably more
efficient) not to have to do this.
Regards,