473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

restricted templates, enum

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
Jul 19 '05 #1
1 6538
"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote...
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.
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

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing...
6
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally...
7
by: DaKoadMunky | last post by:
<CODE> template<int N> int ReturnN() { return N; } int anInt = 32;
1
by: Ben Finney | last post by:
Howdy all, I'd like to have an Abstract Data Type for a scalar value that is restricted to a small set of values. Like an Enum, I suppose. What I would like is to be able to use simple 'str'...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
7
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
5
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a...
4
by: zfareed | last post by:
#include <iostream> #include <fstream> using namespace std; template<class ItemType> class SortedList { private:
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.