473,804 Members | 3,464 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_itera tor_tag {};
struct const_iterator_ tag {};

// traits
template <typename T,
typename tag=non_const_i terator_tag>
struct Iter_traits
{
typedef random_access_i terator_tag iterator_catego ry;
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_i terator_tag iterator_catego ry;
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_i terator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_typ e 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_itera tor_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_itera tor_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>::i terator Container<T>::b egin(void)
{
...
}

The compiler will give a warning:
`typename Container<T>::i terator' 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 2394
"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_itera tor_tag {};
struct const_iterator_ tag {};

// traits
template <typename T,
typename tag=non_const_i terator_tag>
struct Iter_traits
{
typedef random_access_i terator_tag iterator_catego ry;
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_i terator_tag iterator_catego ry;
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_i terator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_typ e 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_itera tor_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_iterato r'.
Those two are different types.
return 0;
}

An error occurs while compliation.

`int* Iter<int, non_const_itera tor_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>::i terator Container<T>::b egin(void)
{
...
}

The compiler will give a warning:
`typename Container<T>::i terator' 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<typena me T>
typename
Container<T>::i terator Container<T>::b egin()
{
...
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_i terator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_typ e 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_itera tor_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>::i terator Container<T>::b egin(void)
{
...
}

The compiler will give a warning: `typename Container<T>::i terator' 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_itera tor wihile the destination class
is a const_iterator
Iter(const Iter<T, non_const_itera tor_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_iterato r'.
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_i terator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_typ e 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_itera tor_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_i terator_tag>
class Iter
{
typedef typename Iter_traits<T, tag>::value_typ e 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_itera tor_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
1698
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 to edit the traits and have the traits form appear instead of the space left for my CustomWidget. wxGlade produces code with the following hook (last line):
0
3753
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 ============================= #include <vector> #include <list> #include <set> #include <functional>
12
2443
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 use ~char_traits<char_type>::eof()? char_traits<char_type>::int_type()? char_traits<char_type>::eof() + 1? while(!char_traits<char_type>::not_eof(char_traits<char_type>::int_type(rand())))? mark
2
3690
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 couldnt get too far - especially on how to handle a control event ? Also while i am on the topic: say i have a list "control" that create using the following two lines:
9
2647
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 simple straightforward way that a simple guy from "Missourah" can understand ?
2
1733
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? Wouldn't it be better for containers to take a policy template argument and for the policy to take a traits template argument? Consider an allocator (policy) that is going to be used in a multi-threaded environment. The programmer could use...
0
1089
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? Wouldn't it be better for containers to take a policy template argument and for the policy to take a traits template argument? Consider an allocator (policy) that is going to be used in a multi-threaded environment. The programmer could use...
2
3413
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
2893
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 tagName;
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4282
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 we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.