473,387 Members | 1,859 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,387 software developers and data experts.

template argument to enum


A)

template <typename scalar, int size>
struct basevector
{
enum { size = size };
scalar v[size];
};

B)

template <typename scalar, int size>
struct vector : basevector<scalar,size>
{
// ... impl.
};

template <typename scalar>
struct vector<scalar,3> : basevector<scalar,3>
{
// ... specialization related enhancements to impl.
};
OK, the code snips out of the way, the A) segment above declares/defines
base type, from which various different vector types are inherited (vector,
point, quaternion...). The B) segment declares the specific example of
scalar vector type. Why there is enum for size in the basevector, is, that
it makes the size visible to specialized types, which don't have access (as
far as I know of) to the template size argument.

Question: is it legal to write enum { size = size }; .. as far as I
undertand, the scope for left side of assignment is the enumeration, and
right side is the outer scope, in this case template argument. Is this
defined well enough in the standard, so that I can expect compilers not to
give compilation time diagnostic warnings about unsolved resolution and such
(I know these are implementation defined details, but does standard guaratee
the above code snip should compile (assuming there aren't typoes above, I
didn't quite copy-paste from complete code, tried to isolate the code
relevant to the question only).

This is a moot point in practise, I gave the outer template argument name
"vsize", but I am asking for curiosity (it compiled with few compilers I
tested on with the above variation).

If someone is wondering why I want the size visible, it is for recursive
template meta-program which requires the size of the vector as one of the
arguments, to know the number of recursions required to handle all
components of the vector. I'm using same "sourcecode path" to generate all
the specializations, and that requires the "size" enumeration, so that I
don't have to repeat the code 9 times (at this time for: vector<scalar,2>,
vector<scalar,3>, vector<scalar,4>, same for quaternion and point, total: 9
times same code, so it's handy to have the enumeration visible, no? :)

I'm assuming that the enum { size = size }; is correct and valid c++, but
somehow it looks amazingly stupid, so I renamed the argument so that the
code wouldn't be so bloody obfuscated to the casual observer. What would you
have done in similiar situation? :)

p.s. namespace collision are easily avoided, since I use these templates
through typedefs, which also have a scope themselves, so in practise it
turns into:

using coremath::float3;
....
float3 v(x,y,z);
float3 u = v * 1.2f + coremath::dot(a,b) * c; // ... etc.

(so apologies, a lot of text for a very short question, is that stupid or
not, if it's stupid, why? :)
Jul 19 '05 #1
7 6166

"wogston" <so*****@microsoft.com> wrote in message
news:bp**********@phys-news1.kolumbus.fi...

A)

template <typename scalar, int size>
struct basevector
{
enum { size = size }; This is ill-formed. You may not redeclare template parameter.
scalar v[size];
};

B)

template <typename scalar, int size>
struct vector : basevector<scalar,size>
{
// ... impl.
};

template <typename scalar>
struct vector<scalar,3> : basevector<scalar,3>
{
// ... specialization related enhancements to impl.
}; [...]
If someone is wondering why I want the size visible, it is for recursive I am not, but I see another problem. The base class scope is not considered
during the name lookup until the point of instantiation of the class. So,
provided you have change the enumerator name to vsize you will have to
qualify vsize explicitly.

[...]
I'm assuming that the enum { size = size }; is correct and valid c++, but

It looks like to be the wrong assumption.

--
Michael Kochetkov.
Jul 19 '05 #2
> > I'm assuming that the enum { size = size }; is correct and valid c++,
but
It looks like to be the wrong assumption.


It looks to me that your not helping much with that. Why is is wrong
assumption, I already explained why I think it was correct, simply saying:
"no, it's wrong" doesn't help much. Tell me what's wrong with the
hypothesis:

template <int x>
struct foo
{
enum { x_ = x };
};

x_ is "deeper" in the scope hierarchy, the x is visible until (and after) x_
is declared. If they have the same name, the scoping rules certainly still
apply? If not, why?

My guess is that you mean, that compiler cannot resolve which x is meant,
but in this case I thought the hierarchically "closer" one overrides
automatically, if not, then say so and I can be sure. I asked this question
in my original post.

I tested with Comeau online compiler, Intel C++ 7.1, Visual C++ .NET 2003
and G++ 3.3 and they all according to you are not standard conformant.
Doesn't look very good for C++ programming in general if the most common
tools are broken. Why is it, that it's always the C++ where tools are broken
and other languages have perfectly working tools, is C++ too complex for
it's own good, to be correctly implemented? Afterall, C++ been around for
years and years and years now, still no good compilers. I'm still waiting.
Someday.

So what's the rationale behind your response? I already gave mine, twice.
Thank you.
Jul 19 '05 #3
wogston wrote:
I tested with Comeau online compiler, Intel C++ 7.1, Visual C++ .NET
2003 and G++ 3.3 and they all according to you are not standard
conformant.


Does that mean that they accepted it? That's strange. Both g++ 3.3 and
comeau online (I can't test the others) reject it here. When I try to
compile the following code:

template <int x>
struct foo
{
enum { x = x };
};

g++ 3.3 gives me the following errors:

wogston.cpp:4: error: declaration of `int x'
wogston.cpp:1: error: shadows template parm `int x'
wogston.cpp:4: error: declaration of `int x'
wogston.cpp:1: error: changes meaning of `x' from `int x'

and comeau online:

"ComeauTest.c", line 4: error: template parameter "x" may not be
redeclared in this scope
enum { x = x };
^

How did you test it?

Jul 19 '05 #4

"wogston" <so*****@microsoft.com> wrote in message
news:bp**********@phys-news1.kolumbus.fi...
I'm assuming that the enum { size = size }; is correct and valid c++,

but
It looks like to be the wrong assumption.


It looks to me that your not helping much with that. Why is is wrong
assumption, I already explained why I think it was correct, simply saying:

I have explained "why" in the lines you have carefully skipped.

--
Michael Kochetkov.
Jul 19 '05 #5
> I have explained "why" in the lines you have carefully skipped.

Ah, sorry, I need a pair of new glasses: I didn't notice with quick glance
that you don't leave blank lines. I saw it as fuzz of black with a quick
*glance*, totally my bad.
Jul 19 '05 #6
> How did you test it?

Not with that trivial example, it fails here, too. I changed the code to:

template <int x>
struct foo { ... };

template <>
struct foo<4> { enum { x = 4 }; };

... I used that style already very quickly after the initial post, I didn't
like the vsize/size syntax either, this is 'nicer' for my purposes, eg for
specialized types I 'supply' the size, or in this case, x.

That should be ok?
Jul 19 '05 #7
wogston wrote:
How did you test it?


Not with that trivial example, it fails here, too. I changed the code
to:

template <int x>
struct foo { ... };

template <>
struct foo<4> { enum { x = 4 }; };

.. I used that style already very quickly after the initial post, I
didn't like the vsize/size syntax either, this is 'nicer' for my
purposes, eg for specialized types I 'supply' the size, or in this
case, x.

That should be ok?


I'd say yes. In the specialization, there is no template parameter x
anymore, so there is no conflict about that name.

Jul 19 '05 #8

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

Similar topics

2
by: Valeriu Catina | last post by:
Hi, given the next code: #define ENUM_CAST(x) int(x) template<class T,int N> class Array {
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
13
by: Imre | last post by:
Please take a look at the following code: template <typename T, typename Enable = void> class A { public: enum { value = 0 }; }; template <typename T>
1
by: Imre | last post by:
Let's suppose we have a primary template with one argument defined in a header file. Two source files include this header, and both define a specialization of the primary template. Later, both...
8
by: Klaus Schneider | last post by:
Hi all! I'm having trouble with a template function with variable arguments when I parse an enum type as variable argument. Example: template <class T> bool test(int num, ...) { va_list ap;...
1
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
6
by: Tooc | last post by:
I have defined a class named Rectangle<typename T>. How do I test for a Rectangle? I just want to know if an object is any type of Rectangle. My code compiles for typeid(Rectangle<>) only if I...
5
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.