Container Requirements for User Defined Types

What exactly are the requirements for a template container class to hold a user defined type? I'm currently in a data structures course, and we have implemented primitive template versions of vector, list, stack, and deque. However, my professor said that all our classes should not be used for user-defined types like classes or structs. I asked why, and he had no answer! I cannot find online any list of requirements or in my book.

If anybody has any information on this, or can point to it?

Thanks
- You must call constructors on object creation and destructors on deletion
- You must not do any bulk memory operations. IE: "dumb" copies like memcpy
- You must not move an object in memory without notifying it (an object can have its 'this' pointer suddenly). For example, when vector needs to reallocate memory, it will actually copy the objects (with the copy ctor) then destroy the original ones. (Or at least it used to. I think C++11 has a move ctor which can be used instead of the copy ctor)

Failure on any of those would result in your container "breaking" the class it is holding.

Exception safety is another issue. Object constructors might throw exceptions and if they do you would have to catch them and have your container clean up (possibly unwind and destroy any previously constructed objects) to prevent leaks.
Last edited on
I'm interpreting this question as "When should I declare my class as a template?"

If this is what you mean to ask then the answer is anytime you have an operation that can be used regardless of datatype. The STL Containers are perfect examples because they are all dynamic arrays, and making a dynamic array is an operation that you can and would want to perform on any number of datatypes including ones you haven't even made yet. The STL Algorythms are another example, things like finding the beginning or end of an array or filling it with a particular value, don't require you to know the datatype of the variables until you go to use the function, so a template is perfect in this scenario as well.

I like to guess... ;-)

since you are writing your own containers, that means it might be not designed for normal classes. For containers in STL of the industry, they are designed to use with any types.
@Disch
- You must call constructors on object creation and destructors on deletion


So, to call a destructor of a template type, is it done like so:
1
2
3
4
5
6
7
8
9
template< class T >
class MyContainer
{
    ~MyContainer()
    {
        ~T(); //like this? Will that work for user-defined types,
                 //as well as built in types like int?
    }
};


You also stated that you must call constructors on object creation...

What if the user class does not define a default constructor or disables some? Like this:

1
2
3
4
5
6
7
8
9
10
class UserDefined
{
public:
    UserDefined(int, char, bool);

private: //disable these 
    UserDefined();
    UserDefined(const UserDefined &other);
    UserDefined& operator=(const UserDefined &other);
}


How does the template class know what constructor to call on creation?
Won't disabling the copy constructor in UserDefined break the reallocation necessary?

Thank you for any help you can give!
Considering the knowledgeable answer of the professor, I'm guessing the course level isn't too advanced and the answer to your question is probably even simpler than the ones above:

Many/All operations on a data structure require the clear definition of several basic operators. The best example for this are the Comparison ('==', '<', '>') operators. Imagine a class Person with members "string Name" and "int Age". When is PersonA > PersonB? There is no "default" answer. Thus, trying to (for example) build a BST of Person objects is impossible, until a clear definition for the operators is given.
rem45acp wrote:
How does the template class know what constructor to call on creation?
all stl containers assume that the default constructor is available. if not a compiler error is emitted.

What a template needs must be documented otherwise the user can't tell (which may lead to hard to read compiler errors)
Topic archived. No new replies allowed.