restricted templates, enum

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
 
V

Victor Bazarov

Alexander Stippler said:
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?

I think you answered your own question: don't use unnamed enums.
As soon as you give a name to the enum, the code compiles fine.

The cause is probably as simple as 14.3.1/2. An unnamed type shall
not be used as a template argument for a type-parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top