A
Alexander Stippler
Hello,
some time ago I learnt much about restricting template parameters by
checking some properties upon these and exploiting SFINAE. But the
existence of unnamed enum types makes me doubt if this approach can be
helpful in a general project. An example:
I have an operator/(), which I would like to overload for vector/T, where T
is long, double, .... I want to be able to use any type, which is
convertible to vector, so I use SFINAE and write something sematically
like:
template <class A, typename B>
typename restrictTo<(isConvertible<A, vector>::value &&
isNumericalType<B>::value), vector>::type
operator/(const A &a, const B &b){ ... }
Soemthing like that works fine.
But as soon as you have an unnamed enum anywhere in your code and a value of
this enum is parameter to "/", you have lost - the compiler emits an error.
For example, the STL implementation of the Intel C++-compiler has such
code.
Why an error?
You would want to have builtin operator/() chosen for the enum. But the
template version is actually taken - due to overload resolution. And here
the error takes place: the compiler tries to instantiate a template
parameter with an unnamed type -> error.
And this will happen, as soon as you have some 'restricted template'
operator/(). So forget restricted templates for operators or what? We can't
influence, if users make use of unnamed enums. Is there a way to get around
this trouble? A short example program, which causes the error (on como):
enum { red, green, blue };
template <typename T, typename S>
int
operator+(const T &, const S &)
{
return 0;
}
int
main()
{
int i = 1;
i = i+green;
return 0;
}
regards,
alex
some time ago I learnt much about restricting template parameters by
checking some properties upon these and exploiting SFINAE. But the
existence of unnamed enum types makes me doubt if this approach can be
helpful in a general project. An example:
I have an operator/(), which I would like to overload for vector/T, where T
is long, double, .... I want to be able to use any type, which is
convertible to vector, so I use SFINAE and write something sematically
like:
template <class A, typename B>
typename restrictTo<(isConvertible<A, vector>::value &&
isNumericalType<B>::value), vector>::type
operator/(const A &a, const B &b){ ... }
Soemthing like that works fine.
But as soon as you have an unnamed enum anywhere in your code and a value of
this enum is parameter to "/", you have lost - the compiler emits an error.
For example, the STL implementation of the Intel C++-compiler has such
code.
Why an error?
You would want to have builtin operator/() chosen for the enum. But the
template version is actually taken - due to overload resolution. And here
the error takes place: the compiler tries to instantiate a template
parameter with an unnamed type -> error.
And this will happen, as soon as you have some 'restricted template'
operator/(). So forget restricted templates for operators or what? We can't
influence, if users make use of unnamed enums. Is there a way to get around
this trouble? A short example program, which causes the error (on como):
enum { red, green, blue };
template <typename T, typename S>
int
operator+(const T &, const S &)
{
return 0;
}
int
main()
{
int i = 1;
i = i+green;
return 0;
}
regards,
alex