473,729 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class with functions that return STL containers with incomplete types

Greetings,

Having classes with member objects that have STL containers of objects
whose definitions are incomplete results in undefined behavior. See
for example:

http://www.ddj.com/database/184403814#8
http://www.parashift.com/c++-faq-lit...html#faq-39.14

I am wondering: is it okay to have member functions that return an STL
container with an incomplete type? My member objects do not contain
incomplete types. For instance, would the following code be OK?
Thanks. --Omari

#include <boost/shared_ptr.hpp>
#include <vector>

class HasSelf
{
public:
HasSelf(std::ve ctor<HasSelf>& contents);
std::vector<Has SelfgetContents () const;

private:
std::vector<boo st::shared_ptr< HasSelf _contents;
};
Jan 9 '08 #1
3 2065
massysett <Or***********@ gmail.comwrote in
news:31******** *************** ***********@h11 g2000prf.google groups.com:
Greetings,

Having classes with member objects that have STL containers of objects
whose definitions are incomplete results in undefined behavior. See
for example:

http://www.ddj.com/database/184403814#8
http://www.parashift.com/c++-faq-lit...sues.html#faq-
39
.14

I am wondering: is it okay to have member functions that return an STL
container with an incomplete type? My member objects do not contain
incomplete types. For instance, would the following code be OK?
Thanks. --Omari

#include <boost/shared_ptr.hpp>
#include <vector>

class HasSelf
{
public:
HasSelf(std::ve ctor<HasSelf>& contents);
std::vector<Has SelfgetContents () const;

private:
std::vector<boo st::shared_ptr< HasSelf _contents;
};
shared_ptr<does not require that the class be complete, but in this
case, shared_ptr itself is complete. Therefore you can have a vector of
shared_ptrs to an incomplete class. I assume that since the member is a
vector of shared_ptrs, that the other vectors are also vectors of
shared_ptrs?
joe
Jan 9 '08 #2
Joe Greer wrote:
massysett <Or***********@ gmail.comwrote in
news:31******** *************** ***********@h11 g2000prf.google groups.com:
>Greetings,

Having classes with member objects that have STL containers of
objects whose definitions are incomplete results in undefined
behavior. See for example:

http://www.ddj.com/database/184403814#8
http://www.parashift.com/c++-faq-lit...sues.html#faq-
39 .14

I am wondering: is it okay to have member functions that return an
STL container with an incomplete type? My member objects do not
contain incomplete types. For instance, would the following code be
OK? Thanks. --Omari

#include <boost/shared_ptr.hpp>
#include <vector>

class HasSelf
{
public:
HasSelf(std::ve ctor<HasSelf>& contents);
std::vector<Has SelfgetContents () const;

private:
std::vector<boo st::shared_ptr< HasSelf _contents;
};

shared_ptr<does not require that the class be complete, but in this
case, shared_ptr itself is complete. Therefore you can have a vector
of shared_ptrs to an incomplete class. I assume that since the
member is a vector of shared_ptrs, that the other vectors are also
vectors of shared_ptrs?
That would require at least creating them using some kind of memory
management (like 'new'), which is more expensive and less efficient
than letting a 'vector' handle the entire array of those. Of course,
if legality is the primary concern, it seems like the only viable
solution...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 9 '08 #3
On Jan 9, 9:38 pm, Joe Greer <jgr...@doublet ake.comwrote:
massysett <OriginalOm...@ gmail.comwrote
innews:31****** *************** *************@h 11g2000prf.goog legroups.com:
Having classes with member objects that have STL containers of objects
whose definitions are incomplete results in undefined behavior. See
for example:
http://www.ddj.com/database/184403814#8
http://www.parashift.com/c++-faq-lit...html#faq-39.14
I am wondering: is it okay to have member functions that return an STL
container with an incomplete type? My member objects do not contain
incomplete types. For instance, would the following code be OK?
#include <boost/shared_ptr.hpp>
#include <vector>
class HasSelf
{
public:
HasSelf(std::ve ctor<HasSelf>& contents);
std::vector<Has SelfgetContents () const;
private:
std::vector<boo st::shared_ptr< HasSelf _contents;
};
shared_ptr<does not require that the class be complete, but
in this case, shared_ptr itself is complete. Therefore you
can have a vector of shared_ptrs to an incomplete class. I
assume that since the member is a vector of shared_ptrs, that
the other vectors are also vectors of shared_ptrs?
If I understand correctly, the vector isn't his problem. He
knows that that's OK. The problem is the parameter of the
constructor and the return value of getContents.

If I interpret the situation correctly: according to the
standard, it is undefined behavior to instantiate vector with an
incomplete type (and HasSelf only becomes complete at the
closing brace). However (§14.7.1/1), "[...], the class template
is implicitly instantiated when the specialization is referenced
in a context that requires a completely-defined object type or
when the completeness of the class type affects the semantics of
the program." Since a reference or a return value may be an
incomplete type; the compiler should not instantiate the
template in this case, so the code is correct. (On the other
hand, converting between vector< HasSelf and vector<
shared_ptr< HasSelf is typically not a trivial operation.
Quite frankly, I think I'd just use a std::vector< HasSelf >* as
the member.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 10 '08 #4

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

Similar topics

2
2464
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
9
2316
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
6
5875
by: eselk | last post by:
If I have: class A { public: class some_base_class **Obj; }; And I would like to redefine "Obj" in a class derived from class A, something like this maybe:
3
1592
by: KK | last post by:
Hello all, I have several classes binded by one common interface - say 'sum' interface which calculates the sum of all the class elements of type 'int'. class Alphabet { int _a; int _b; int _c;
5
2069
by: Tom | last post by:
If I have a container class that has a map member which stores pointers to objects that have been created via the new operator and I have a method that returns a entry in the map, would it be best to return the entry as a dereferenced pointer so it becomes a class or return a reference to the dereferenced class? I believe that it would be best to dereference the class pointer retrieved from the map and then send it back as a reference to...
14
2638
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
15
3532
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
12
6130
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: class SuperClass { const CNST = 'Super class'; public static function getCnst () {
55
3979
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled. Is all that complexity worth it just to get built-in-like behavior from class objects? Maybe a...
0
8917
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
8761
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
9142
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
8148
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
6722
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...
0
6022
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
4525
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2163
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.