tags:

views:

194

answers:

4

I want to make a generic stack using the c++ templates. The prototype of the push method of stack is given by Void push( t* ptr) Where t is the template argument. Now the pointer ptr may point to an integer, or an array of integers, it may point to a single character or array of characters , it may point to a single double number or an array of doubles etc. What I want is , I want to allocate the memory inside the push method if ptr is pointing to an integer then I need to allocate memory with respect to the size of integer, if ptr is pointing to array of integers then I need to allocate the memory according to the size of the array. Similarly for the other primitive data types My question is how to determine that this pointe ptr is pointing to an array or a simple variable. If it is not possible to find out that a pointer is pointing to an array or a simple variable, then suggest a technique by which I can write the push method of this stack.

A: 

It is not possible to deduce the size of an array pointed to by a T*, once the array has "decayed" to a pointer.

Consider having your stack type take a pair of pointers, one to the first element in the array, and one to the last element in the array. Or, have an overloaded push method - one which takes a single pointer, and one which takes a single pointer and size_t telling you how many objects are pointed to.

And I'm a little confused as to why you have a T* as the parameter. Why not take a T, like most "add" operations on stl containers? And while you're at it, do you know that std::stack exists (it's a "container adapter" in the stl)? Even if you have a good reason to re-implement it, looking to the STL for API design examples is a good idea.

Terry Mahaffey
+2  A: 

What you are suggesting is not a good idea - it is not possible in C++ to detect if a pointer points to a single instance or an array, but the two need to be treated differently.

Personally, I would use the std::stack adaptor that is part of the C++ standard.

anon
A: 

your compiler already takes care of the allocation, and the copying. If you define push like so:

push(T item)

The compiler will copy the item for you and you can stuff it into your internal storage.

Also, int and int[] are two distinct types. If you wanted to specifically make a stack that could accept an T[] you could make an overload of push:

push(T[] items)

I'm assuming you want this functionality so that you can push these as individual items onto the stack, not because you want a heterogeneous container. In this push you could iterate over each item in the array and store it into your internal storage.

joshperry
A: 

You can use partial specialization of template like this for stack of arrays:

template <typename T>
class Stack <T*>
{
 //other stuff
};

But I am not sure that your compiler supports it or not since partial template specilization is not supported in vc++ 7.0 ,later version may have support of it .

I far as i know linux gnu 8.2 GNU c++ supports it.

Ashish