473,503 Members | 11,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

traits help

Hi there,
I am learning template programming and there is a problem about
traits. Now consider a container and an iterator. Here is the code

// tag for const iterator and non-const iterator
struct non_const_iterator_tag {};
struct const_iterator_tag {};

// traits
template <typename T,
typename tag=non_const_iterator_tag>
struct Iter_traits
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};

// For const iterator ...
template <typename T>
struct Iter_traits<T, const_iterator_tag>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
};
//Iter is an iterator

template <typename T, typename tag=non_const_iterator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_type value_type;
typedef typename Iter_traits<T, tag>::reference reference;
public:
Iter(T *c) :cont(c) {...}

// copy constructor to shift the non-const iterator to const
iterator
Iter(const Iter<T, non_const_iterator_tag>& it) :cont(it.c)
{...}

private:
T *cont;
};

// Container is a container.

template <typename T>
class Container
{
...
public:
typedef Iter<T> iterator;
typedef Iter<T, const_iterator_tag> const_iterator;

iterator begin() {...};
const_iterator begin() const {...};
iterator end() {...};
const_iterator end() const {...};
private:
T *data;
...
};

int main(void)
{
Container<int> c;

// copy constructor should be actived here
Container<int>::const_iterator cit=c.begin();
return 0;
}

An error occurs while compliation.

`int* Iter<int, non_const_iterator_tag>::cont' is private

I don't know why. It seems that it's due to the copy constructor.
Please help!
BTW, if I define begin() outside the class. e.g.

template <typename T>
Container<T>::iterator Container<T>::begin(void)
{
...
}

The compiler will give a warning:
`typename Container<T>::iterator' is implicitly a typename
mplicit typename is deprecated, please see the documentation for
details

Is that anything wrong? Should I do something to prevent that warning?

Thanks in advance.
Jul 19 '05 #1
5 2382
"Rex_chaos" <re*******@21cn.com> wrote...
I am learning template programming and there is a problem about
traits. Now consider a container and an iterator. Here is the code

// tag for const iterator and non-const iterator
struct non_const_iterator_tag {};
struct const_iterator_tag {};

// traits
template <typename T,
typename tag=non_const_iterator_tag>
struct Iter_traits
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};

// For const iterator ...
template <typename T>
struct Iter_traits<T, const_iterator_tag>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
};
//Iter is an iterator

template <typename T, typename tag=non_const_iterator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_type value_type;
typedef typename Iter_traits<T, tag>::reference reference;
public:
Iter(T *c) :cont(c) {...}

// copy constructor to shift the non-const iterator to const
iterator
Why do you say "to shift the ..."? It's just a copy constructor.
Iter(const Iter<T, non_const_iterator_tag>& it) :cont(it.c)
{...}

private:
T *cont;
};

// Container is a container.
The comment above seems unnecessary. The class name is self-
documenting.

template <typename T>
class Container
{
...
public:
typedef Iter<T> iterator;
typedef Iter<T, const_iterator_tag> const_iterator;

iterator begin() {...};
const_iterator begin() const {...};
iterator end() {...};
const_iterator end() const {...};
Drop the semicolons after the closing curly braces. The code
looks dirty due to them.
private:
T *data;
...
};

int main(void)
{
Container<int> c;

// copy constructor should be actived here
Container<int>::const_iterator cit=c.begin();
Why should it? c.begin() returns 'Container<int>::iterator'.
You want to assign it to 'Container<int>::const_iterator'.
Those two are different types.
return 0;
}

An error occurs while compliation.

`int* Iter<int, non_const_iterator_tag>::cont' is private

I don't know why. It seems that it's due to the copy constructor.
No. It's due to the fact that you don't have a constructor that
constructs a const_iterator from a "regular" iterator.
Please help!
Define a parameterised constructor that would convert 'iterator'
into 'const_iterator'.


BTW, if I define begin() outside the class. e.g.

template <typename T>
Container<T>::iterator Container<T>::begin(void)
{
...
}

The compiler will give a warning:
`typename Container<T>::iterator' is implicitly a typename
mplicit typename is deprecated, please see the documentation for
details

Is that anything wrong? Should I do something to prevent that warning?


You should declare such function this way:

template<typename T>
typename
Container<T>::iterator Container<T>::begin()
{
...
Victor
Jul 19 '05 #2
On 23 Oct 2003 13:22:26 -0700, Rex_chaos <re*******@21cn.com> wrote:
Hi there,
//Iter is an iterator

template <typename T, typename tag=non_const_iterator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_type value_type;
typedef typename Iter_traits<T, tag>::reference reference;
public:
Iter(T *c) :cont(c) {...}

// copy constructor to shift the non-const iterator to const
iterator
Iter(const Iter<T, non_const_iterator_tag>& it) :cont(it.c)
{...}

this part looks suspicious. I don't know what it.c is but it is probably
private and
const_iterator do not have access to it .


private:
T *cont;
};

// Container is a container.

I don't know why. It seems that it's due to the copy constructor.
Please help!
BTW, if I define begin() outside the class. e.g.

template <typename T>
Container<T>::iterator Container<T>::begin(void)
{
...
}

The compiler will give a warning: `typename Container<T>::iterator' is
implicitly a typename
mplicit typename is deprecated, please see the documentation for
details

Is that anything wrong? Should I do something to prevent that warning?

Thanks in advance.


--
grzegorz
Jul 19 '05 #3
> Why do you say "to shift the ..."? It's just a copy constructor.
Yes. But the copy constructor is not a common one, because the
incomming class is a non_const_iterator wihile the destination class
is a const_iterator
Iter(const Iter<T, non_const_iterator_tag>& it) :cont(it.c)
{...}

private:
T *cont;
};

template <typename T>
class Container
{
...
public:
typedef Iter<T> iterator;
typedef Iter<T, const_iterator_tag> const_iterator;

iterator begin() {...};
const_iterator begin() const {...};
iterator end() {...};
const_iterator end() const {...};
Drop the semicolons after the closing curly braces. The code
looks dirty due to them.

Sorry, I forgot to erase the semicolons.
private:
T *data;
...
};

int main(void)
{
Container<int> c;

// copy constructor should be actived here
Container<int>::const_iterator cit=c.begin();


Why should it? c.begin() returns 'Container<int>::iterator'.
You want to assign it to 'Container<int>::const_iterator'.
Those two are different types.

Take a look at the Container<T> class, I have two functions for
begin(), one returns the iterator and other returns the
const_iterator. When I call
Container<int>::const_iterator cit=c.begin();
I hope the later begin() will be called. However, it won't. So I have
a copy constructor in my code.

No. It's due to the fact that you don't have a constructor that
constructs a const_iterator from a "regular" iterator.

But I have a copy constructor for this purpose.
Jul 19 '05 #4
> >
template <typename T, typename tag=non_const_iterator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_type value_type;
typedef typename Iter_traits<T, tag>::reference reference;
public:
Iter(T *c) :cont(c) {...}

// copy constructor to shift the non-const iterator to const
iterator
Iter(const Iter<T, non_const_iterator_tag>& it) :cont(it.c)
{...}

this part looks suspicious. I don't know what it.c is but it is probably
private and
const_iterator do not have access to it .

Try this code, and you see that in (copy) constructor, the class
itself can also access the private data.

class Test
{
public:
Test() {...}
Test(const Test& t) :data(t.data) {}
private:
int data;
};
Jul 19 '05 #5
"Rex_chaos" <re*******@21cn.com> wrote...

template <typename T, typename tag=non_const_iterator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_type value_type;
typedef typename Iter_traits<T, tag>::reference reference;
public:
Iter(T *c) :cont(c) {...}

// copy constructor to shift the non-const iterator to const
iterator
Iter(const Iter<T, non_const_iterator_tag>& it) :cont(it.c)
{...}

this part looks suspicious. I don't know what it.c is but it is probably
private and
const_iterator do not have access to it .

Try this code, and you see that in (copy) constructor, the class
itself can also access the private data.

class Test
{
public:
Test() {...}
Test(const Test& t) :data(t.data) {}
private:
int data;
};


In this code, in the copy c-tor, 't' and '*this' are of the
_same_ type. In your attempt, iterator and const_iterator
are not even related.

Victor
Jul 19 '05 #6

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

Similar topics

1
1682
by: Donald 'Paddy' McCarthy | last post by:
Hi, I am having a few problems with a GUI. I am new to traits and wxGlade. I have used wxGlade to create a Form with an embedded space for a CustomWidget. I have the traits demo and would like...
0
3736
by: skscpp | last post by:
What's wrong with the following code? Compiler error is at the bottom. The compiler I am using is gcc version 2.95. ============================= // traits.h =============================...
12
2405
by: Mark A. Gibbs | last post by:
Good day, What is the safest way to get a non-end of file value? char_traits<char_type>::eof() will get me eof, but how can I safely and consistently generate a value that is not eof? should i...
2
3674
by: Ash | last post by:
Hello everyone ! I am trying to find some sort of a cookbook or more examples for using Enthought Traits to build GUI's. I tried to follow the documentations present at the enthought site, but...
9
2622
by: Bit Byte | last post by:
Can't seem to get my head around the point of a trait class - no matter how many times I read up on it - why not simply use functors or function pointers ? Anyone care to explain this in a...
2
1712
by: Milburn Young | last post by:
I see the STL using class templates accepting a traits and a policy. To me, the traits are the "what" and the policy is the "how". How can a policy know what to do without the traits of the topic?...
0
1073
by: Milburn Young | last post by:
I see the STL using class templates accepting a traits and a policy. To me, the traits are the "what" and the policy is the "how". How can a policy know what to do without the traits of the topic?...
2
3397
by: Colin J. Williams | last post by:
Using >easy_install -v -f http://code.enthought.com/enstaller/eggs/source enthought.traits The result is: .... many lines ....
5
2872
by: greek_bill | last post by:
Hi, I'm trying to develop a system where I can register some data/ information about a class. For example // ClassInfo.h template <class T> struct ClassInfo { static const std::string ...
0
7095
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
7294
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,...
1
7015
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
7470
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...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.