Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typedef & template

Reply
Thread Tools

typedef & template

 
 
Dmytro
Guest
Posts: n/a
 
      06-18-2006
Hi!

This code is ok:

template<class T>
struct Container
{
T x;
typedef int size_type;
size_type size() { return sizeof T; }
};

Why the compiler (VC7) does not compile the following code?

template<class T>
struct Container
{
T x;
typedef int size_type;
size_type size() const;
};

template<class T>
Container<T>::size_type Container<T>::size() { return sizeof T; }

---

Thanks

Dmytro

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-18-2006
Dmytro wrote:
> Hi!
>
> This code is ok:
>
> template<class T>
> struct Container
> {
> T x;
> typedef int size_type;
> size_type size() { return sizeof T; }
> };
>
> Why the compiler (VC7) does not compile the following code?
>
> template<class T>
> struct Container
> {
> T x;
> typedef int size_type;


Why use int for a size_type? Does a negative size make sense?

> size_type size() const;
> };
>
> template<class T>
> Container<T>::size_type Container<T>::size() { return sizeof T; }
>

You left out two important things:

typename Container<T>::size_type Container<T>::size() const {
return sizeof T; }

The typename is required to tell the compiler that
Container<T>::size_type is a type and the const because the prototype
function is const.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      06-18-2006
Dmytro <> wrote:

> Why the compiler (VC7) does not compile the following code?
>
> template<class T>
> struct Container
> {
> T x;
> typedef int size_type;
> size_type size() const;
> };
>
> template<class T>
> Container<T>::size_type Container<T>::size() { return sizeof T; }


Where (which line in the above snippet) and which error do you get?
My guess is you're missing a 'typename' in the last line.

hth
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-18-2006
Ian Collins wrote:

> Dmytro wrote:
>> Hi!
>>
>> This code is ok:
>>
>> template<class T>
>> struct Container
>> {
>> T x;
>> typedef int size_type;
>> size_type size() { return sizeof T; }
>> };
>>
>> Why the compiler (VC7) does not compile the following code?
>>
>> template<class T>
>> struct Container
>> {
>> T x;
>> typedef int size_type;

>
> Why use int for a size_type? Does a negative size make sense?


Actually, it does. If you want to be able to get an offset between two
arbitrary elements, that offset must be a signed type and will only be able
to span half of the range of the unsigned size type. So the size couldn't
be more anyway. And then, it's always a bad idea to mix signed and
unsigned, so actually, I would prefer a signed size type.


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      06-18-2006
Rolf Magnus posted:

> And then, it's always a bad idea to mix
> signed and unsigned, so actually, I would prefer a signed size type.



Some people simply used "signed int" all the time. The situation is very
similar to how some people will always use "i++" and have no intention of
changing their style.

I myself only use signed integer types when I have to. Elsewhere, I use
unsigned integer types.



--

Frederick Gotham
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typedef of template<T> x to member x in template class squarewave@blockhead.net C++ 1 02-20-2009 08:15 PM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
Extracting template types from a typedef'd template declaration Adam Nielsen C++ 3 10-19-2007 09:54 AM
Typedef of a template? Richard van Wegen C++ 3 07-15-2003 07:22 AM
template typedef as return type Robert A. T. Kaldy C++ 1 07-09-2003 06:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57