By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

typedef & template

P: n/a
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

Jun 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Jun 18 '06 #2

P: n/a
Dmytro <dm***************@gmail.com> 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)
Jun 18 '06 #3

P: n/a
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.
Jun 18 '06 #4

P: n/a
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
Jun 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.