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

Typedef allows redefinition of names?

I was a bit surprised to find that my compiler (MSVC++) actually
accepts the following as legal. It cleans up the code very nicely,
but I am sort of confused about some of the finer points of how the
naming and template systems interact in a compiler. Does the
following always work?

In Vector3.h, I have this class

namespace Geometry {

termplate<typename NumericType>
class Vector3
{
....
};

}

In another file, Triangle.h, I have

#include <Vector3.h>

namespace Geometry {

template<typename NumericType>
class Triangle
{
public:
typedef Vector3<NumericTypeVector3;

//Then I go on to use Vector3 instead of Vector3<NumericType>
};

}

Like I said, I was pretty surprised it let me take the "root" part of
the template name Vector3<NumericTypeand redefine it as a specific
type, namely the specific type that inherits the template type from
the class that it belongs to. Needless to say, if I type in
Vector3<NumericTypeafter the typedef this produces an error, since
the symbol Vector3 = Vector3<NumericTypealready.

Thanks for your time,
Ken

Nov 5 '07 #1
5 3124
Ken Camann <kj******@gmail.comwrote in news:1194293869.860459.137680@
57g2000hsv.googlegroups.com:
I was a bit surprised to find that my compiler (MSVC++) actually
accepts the following as legal. It cleans up the code very nicely,
but I am sort of confused about some of the finer points of how the
naming and template systems interact in a compiler. Does the
following always work?

In Vector3.h, I have this class

namespace Geometry {

termplate<typename NumericType>
class Vector3
{
...
};

}

In another file, Triangle.h, I have

#include <Vector3.h>

namespace Geometry {

template<typename NumericType>
class Triangle
{
public:
typedef Vector3<NumericTypeVector3;

//Then I go on to use Vector3 instead of Vector3<NumericType>
};

}

Like I said, I was pretty surprised it let me take the "root" part of
the template name Vector3<NumericTypeand redefine it as a specific
type, namely the specific type that inherits the template type from
the class that it belongs to. Needless to say, if I type in
Vector3<NumericTypeafter the typedef this produces an error, since
the symbol Vector3 = Vector3<NumericTypealready.
Interesting...

I suspect that it is because there is no Vector3 defined at that scope
(within the class). So, what you have defined is Geometry::Triangle
<NumericType>::Vector3 and that doesn't conflict with Geometry::Vector3
<>.

joe
Nov 5 '07 #2
Ken Camann wrote:
I was a bit surprised to find that my compiler (MSVC++) actually
accepts the following as legal. It cleans up the code very nicely,
but I am sort of confused about some of the finer points of how the
naming and template systems interact in a compiler. Does the
following always work?

In Vector3.h, I have this class

namespace Geometry {

termplate<typename NumericType>
class Vector3
{
...
};

}

In another file, Triangle.h, I have

#include <Vector3.h>

namespace Geometry {

template<typename NumericType>
class Triangle
{
public:
typedef Vector3<NumericTypeVector3;

//Then I go on to use Vector3 instead of Vector3<NumericType>
};

}

Like I said, I was pretty surprised it let me take the "root" part of
the template name Vector3<NumericTypeand redefine it as a specific
type, namely the specific type that inherits the template type from
the class that it belongs to. Needless to say, if I type in
Vector3<NumericTypeafter the typedef this produces an error, since
the symbol Vector3 = Vector3<NumericTypealready.
I believe it's OK to do that (just like you can define an object of
type 'S' and call it 'S'), but I doubt it's a good idea. I would
recommend using a different identifier for the typedef-name.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 5 '07 #3
Victor Bazarov wrote:
I believe it's OK to do that (just like you can define an object of
type 'S' and call it 'S'), but I doubt it's a good idea. Â*I would
recommend using a different identifier for the typedef-name.
Hmmm...
This:
template<typename Tclass Test {
typedef Test Test<T>;
Test t;
};

does not compile on the g++ version on my computer (v4.1.3):
test.C:2: error: ISO C++ forbids nested type ‘Test’ with same name as
enclosing class
test.C:2: error: ‘Test<T>::Test’ has the same name as the class in which it
is declared

Nov 5 '07 #4
Paul Brettschneider wrote:
Victor Bazarov wrote:
>I believe it's OK to do that (just like you can define an object of
type 'S' and call it 'S'), but I doubt it's a good idea. Â*I would
recommend using a different identifier for the typedef-name.

Hmmm...
This:
template<typename Tclass Test {
typedef Test Test<T>;
Test t;
};

does not compile on the g++ version on my computer (v4.1.3):
test.C:2: error: ISO C++ forbids nested type ‘Test’ with same name as
enclosing class
test.C:2: error: ‘Test<T>::Test’ has the same name as the class in which
it is declared
This, on the other hand, does:
template<typename Tclass Test {
T t;
};

template<typename Tclass Test2 {
typedef Test<TTest;
Test t;
};
Nov 5 '07 #5
Paul Brettschneider wrote:
Victor Bazarov wrote:
>I believe it's OK to do that (just like you can define an object of
type 'S' and call it 'S'), but I doubt it's a good idea. *I would
recommend using a different identifier for the typedef-name.

Hmmm...
This:
template<typename Tclass Test {
typedef Test Test<T>;
Test t;
};
Ouch. This is so wrong it hurts:
* typedef the wrong way 'round.
* class with itself as member object.
Sorry for the noise. I will try to think before posting next time. :/
Nov 5 '07 #6

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

Similar topics

0
by: Imre | last post by:
I'm planning to create a macro-based property system (reflection, automatic serializing for properties, that kind of stuff). The system would involve writing some PROPERTY(propName) macros between...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
8
by: J Krugman | last post by:
My compiler complains if I do something like this typedef struct { node *next; } node; but it's OK with typedef struct superfluous_identifier { superfluous_identifier *next;
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
8
by: Lathe_Biosas | last post by:
Hi While compiling my application that needs "windows.h" there are some typedef redefinition errors redefinition at winnt.h( line 207) typedef void *HANDLE redefinition at windef.h( line...
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
7
by: Michael B Allen | last post by:
I have a forward reference like: struct foo; int some_fn(struct foo *param); Because the parameter is a pointer the compiler is satisfied. But now I wan to change 'struct foo' to a...
45
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.