473,394 Members | 1,642 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 & template

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
4 1979
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arkadiy Vertleyb | last post by:
Hi all, I am having a problem trying to overload a function template, based on a typedef, such as: template<class T> struct A {}; template<class T>
5
by: Roger Leigh | last post by:
Although I've got over most of my template-related problems, I'm having trouble when I started to use default template parameters. For template type T, I've typedef'd this as object_type and then...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Patrick Kowalzick | last post by:
Hello NG, sorry to bother again, but I am a lit surprised that I got no answer on my post (attached below). So I refined the code a little bit :-). If there is a typedefed class X inside a...
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...
5
by: jimmy | last post by:
I am trying to simulate typedef template similar to the suggestion of Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm However when implementing typedef templates according...
5
by: Alan Woodland | last post by:
Hi, I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
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:
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: 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: 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:
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...
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.