M
Martin MacRobert
Hi,
I'm trying to make a specialisation of a template function, so that
the second parameter accepts scalar types only (int,double,float
etc.).
How can I do this without writing an explicit specialisation for all
scalar types? This is because of the large number of functions to
overload.
For example:
// where BinaryExpr is declared
class BinaryExpr:
public Expression< BinaryExpr<Expr1, Expr2, Op> >
{...};
//+ operator General case
template <typename Expr1, typename Expr2>
BinaryExpr< Expr1, Expr2, std:lus >
operator +(const Expression<Expr1>& expr1, const Expression<Expr2>&
expr2)
{...}
// I want to AVOID writing a specialisation for each scalar type as
shown below:
template <typename Expr1>
BinaryExpr< Expr1, int, std:lus >
operator +(const Expression<Expr1>& expr1, int expr2)
{...}
I have a "literal" class that can take a scalar in the constructor.
"Literal" is also derived from "Expression" in a "curious" fashion,
however if I specialise the operator "+" for "Literal", the overload
is ambiguous.
I tried creating a "Scalar" template not derived from Expression, that
has no implementation for non-scalars and fully implemented for each
scalar type. The compiler still could not deduce the appropriate type
for the scalar.
Any help much appreciated.
Thanks,
Martin
I'm trying to make a specialisation of a template function, so that
the second parameter accepts scalar types only (int,double,float
etc.).
How can I do this without writing an explicit specialisation for all
scalar types? This is because of the large number of functions to
overload.
For example:
// where BinaryExpr is declared
class BinaryExpr:
public Expression< BinaryExpr<Expr1, Expr2, Op> >
{...};
//+ operator General case
template <typename Expr1, typename Expr2>
BinaryExpr< Expr1, Expr2, std:lus >
operator +(const Expression<Expr1>& expr1, const Expression<Expr2>&
expr2)
{...}
// I want to AVOID writing a specialisation for each scalar type as
shown below:
template <typename Expr1>
BinaryExpr< Expr1, int, std:lus >
operator +(const Expression<Expr1>& expr1, int expr2)
{...}
I have a "literal" class that can take a scalar in the constructor.
"Literal" is also derived from "Expression" in a "curious" fashion,
however if I specialise the operator "+" for "Literal", the overload
is ambiguous.
I tried creating a "Scalar" template not derived from Expression, that
has no implementation for non-scalars and fully implemented for each
scalar type. The compiler still could not deduce the appropriate type
for the scalar.
Any help much appreciated.
Thanks,
Martin