473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::list of template class

In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:

using std::list;
template < class Contents class Tree_node{

Contents cn;
list < BTree_node < Contents children;

........
}

template < class Contents class Tree{
list < Tree_node < Contents ;

........
}

on what microsoft VC++ compiler complains with error message
error C2079: 'child_list' uses undefined class 'list<class
BTree_node<int> ,class std::allocator< class BTree_node<int >'
when trying to instantiate
Tree <inttree;

What is the workaround for this?
Thanks in advance

Jan 21 '07 #1
7 2973
al*****@pisem.n et wrote:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:

using std::list;
template < class Contents class Tree_node{

Contents cn;
list < BTree_node < Contents children;
Do you mean:

list < Tree_node < Contents children;
^^^^
.......
}

template < class Contents class Tree{
list < Tree_node < Contents ;

.......
}

on what microsoft VC++ compiler complains with error message
error C2079: 'child_list' uses undefined class 'list<class
BTree_node<int> ,class std::allocator< class BTree_node<int >'
when trying to instantiate
Tree <inttree;
You seem to be running into the trap

class X {

std::list< X some_member;

};

This is not required to compile and if it does, the behavior is undefined.
The reason is that X is incomplete in std::list<X>. It is regrettable that
the standard requires completeness for type arguments in std::list<>, but
it does in [17.4.3.6/2].

What is the workaround for this?
(a) Don't bother: fix the typo from BTree_node to Tree_node and hope for the
best. Most implementation of std::list<do actually not require the type
argument to be a complete type. (You might run into trouble with g++
installations that were built with the concept-checks option enabled. So
much for portability.)

(b) Write your own std::list<drop in replacement that, for sure, does not
require completeness. That can be done in about 900 LOC. Or use an STL
implementation that makes this guarantee.

(c) Introduce yet another level of indirection

class X {

std::list<X*som e_ptr;

};

and deal with the memory management issues that arise.

Best

Kai-Uwe Bux

Jan 21 '07 #2
al*****@pisem.n et wrote:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:

using std::list;
template < class Contents class Tree_node{

Contents cn;
list < BTree_node < Contents children;

.......
}

template < class Contents class Tree{
list < Tree_node < Contents ;

.......
}

on what microsoft VC++ compiler complains with error message
error C2079: 'child_list' uses undefined class 'list<class
BTree_node<int> ,class std::allocator< class BTree_node<int >'
when trying to instantiate
Tree <inttree;

What is the workaround for this?
Define class BTree_node.
Jan 21 '07 #3

al*****@pisem.n et wrote:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:

using std::list;
template < class Contents class Tree_node{

Contents cn;
list < BTree_node < Contents children;

.......
}

template < class Contents class Tree{
list < Tree_node < Contents ;

.......
}

on what microsoft VC++ compiler complains with error message
error C2079: 'child_list' uses undefined class 'list<class
BTree_node<int> ,class std::allocator< class BTree_node<int >'
when trying to instantiate
Tree <inttree;

What is the workaround for this?
Deciding whether the name of the class template is Tree_node or
BTree_node would probably help with the compiler problem.

There is some disagreement whether an object may safely have a Standard
container of its own type as one of its data members, or whether the
member declaration of the container is specifying an "incomplete " type.
If so, the program's behavior is, technically at least, undefined.

Greg

Jan 21 '07 #4
i surely did a misprint editing the message - in a compiler there is
BTree_node everywhere

Jan 21 '07 #5

Kai-Uwe Bux wrote:
al*****@pisem.n et wrote:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:

using std::list;
template < class Contents class Tree_node{

Contents cn;
list < BTree_node < Contents children;
.......
}

You seem to be running into the trap

class X {

std::list< X some_member;

};
Unlikely, since the code is question is declaring a class template -
not a class - with the std::list<of its own type as a data member.
This is not required to compile and if it does, the behavior is undefined.
The reason is that X is incomplete in std::list<X>. It is regrettable that
the standard requires completeness for type arguments in std::list<>, but
it does in [17.4.3.6/2].
But in this case, "X" would be a specialization of a class template.
Therefore, the std::list<data member of X would be instantiated - if
it is instantiated at all - only after an instance of the template
class X itself had been instantiated - which of course requires that X
be a complete type. In other words, X must be a complete type before
its std::list data member could ever be instantiated.
What is the workaround for this?

(a) Don't bother: fix the typo from BTree_node to Tree_node and hope for the
best. Most implementation of std::list<do actually not require the type
argument to be a complete type. (You might run into trouble with g++
installations that were built with the concept-checks option enabled. So
much for portability.)
I think the type is the only evident problem at this point.

Greg

Jan 21 '07 #6
Greg wrote:
>
Kai-Uwe Bux wrote:
>al*****@pisem.n et wrote:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:

using std::list;
template < class Contents class Tree_node{

Contents cn;
list < BTree_node < Contents children;
.......
}

You seem to be running into the trap

class X {

std::list< X some_member;

};

Unlikely, since the code is question is declaring a class template -
not a class - with the std::list<of its own type as a data member.
>This is not required to compile and if it does, the behavior is
undefined. The reason is that X is incomplete in std::list<X>. It is
regrettable that the standard requires completeness for type arguments in
std::list<>, but it does in [17.4.3.6/2].

But in this case, "X" would be a specialization of a class template.
Therefore, the std::list<data member of X would be instantiated - if
it is instantiated at all - only after an instance of the template
class X itself had been instantiated - which of course requires that X
be a complete type. In other words, X must be a complete type before
its std::list data member could ever be instantiated.
Templates do not by-pass the completeness problem. Consider

template < typename T >
class X {

std::list< X some_member; // (*)

};

On line (*) the type X is incomplete. You are right to observe that X will
be complete, once X<inthas been instantiated. However [14.6/7] kicks in:

...If a type used in a non-dependent name is incomplete at the point at
which a template is defined but is complete at the point at which an
instantiation is done, and if the completeness of that type affects
whether or not the program is well-formed or affects the semantics of the
program, the program is ill-formed; no diagnostic is required. [Note: if a
template is instantiated, errors will be diagnosed according to the other
rules in this Standard. Exactly when these errors are diagnosed is a
quality of implementation issue. ] ...

Thus, if completeness actually mattered with regard to std::list<>, we would
be doomed nonetheless.

Also, this is to be expected. After all

template < typename T >
class X {

std::pair< X, X data;

};

cannot work just because we turned the recursive type bomb into a template.

What is the workaround for this?

(a) Don't bother: fix the typo from BTree_node to Tree_node and hope for
the best. Most implementation of std::list<do actually not require the
type argument to be a complete type. (You might run into trouble with g++
installation s that were built with the concept-checks option enabled. So
much for portability.)

I think the type is the only evident problem at this point.
Do you mean "type" or "typo"?

Best

Kai-Uwe Bux
Jan 21 '07 #7
Kai-Uwe Bux wrote:
Greg wrote:
What is the workaround for this?

(a) Don't bother: fix the typo from BTree_node to Tree_node and hope for
the best. Most implementation of std::list<do actually not require the
type argument to be a complete type. (You might run into trouble with g++
installations that were built with the concept-checks option enabled. So
much for portability.)
The "workaround " I would suggest would actually be - in my view - a
better overall implementation. The idea would be to store the child
tree nodes in the list as smart pointers. Doing so solves the
"incomplete " type problem since a std::tr1::share d_ptr does not require
a complete type - yet a template instantiated with an incomplete type
is itself a complete type.

Moreover, managing the tree nodes without some kind of shared ptr is
apt to lead to dangling pointers, or leaks, or similar problems.
I think the type is the only evident problem at this point.

Do you mean "type" or "typo"?
The word "type" in this case was a typo for "typo" - so the word was an
example of itself even as it was a word for something else. Glad I was
able to clear that up.

Greg

Jan 22 '07 #8

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

Similar topics

0
2212
by: Sebastian Faust | last post by:
Hi, I am thinking now for a while about a design decision. I would be glad if you can give me some advices which is maybe the better way for solving my problem. I wanna do something like described in the Bridge-Pattern. Therefore I have the following classes: Instrument InstrumentImpl InstrumentData
8
2841
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl list class must Ioverride in order for this to work?
4
2486
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<> with incomplete type? I've seen some STL implementations which allow this and some others that does not. I did not find any mentioning of this topic in the standard, maybe I searched not enough thoroughly?
7
2599
by: Gernot Frisch | last post by:
class A { public: std::list<A> m_a; }; yields errorC2079: '_Value' uses undefined class 'A' using std::vector will work...
44
3871
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be specialized for anything. Thanks, Josh McFarlane
2
1842
by: esuvs | last post by:
Hi, I would like to change the behavior of the std::list so that if I set an iterator to the last element and increment it I would like it to point to the first element, and vice vesa. That is, i would like to use it as a circular buffer. Is this possble? Given that the linked list is probably just a set of nodes and 'next' pointers it doesn't seem unreasonable (just set the last pointer to point at the start) but I suspect the std::list...
2
3923
by: ranin02 | last post by:
Hi, We have a list derived from std::list that has a custom allocator derived from std::allocator. This was originally written using VC++ 6.0 which required a workaround for the fact that 6.0 did not support nested templates classes, so could not use the rebind struct. Instead, a _Charalloc function was implemented. Now that we are moving to VS 2005, this is no longer working so I need to implement using the rebind struct. So I did...
12
2707
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
4185
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new std::list<T>(); } List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); } List(int n, const T& value) { lst = new std::list<T>(n, value); }
0
8944
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
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9306
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
9234
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
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
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.