Fully Generic C++ Stack Implementation Without Lists, Arrays, or Vectors I'm trying to write a fully generic Stack in C++.  I'm not sure if this is the best way to do this.  

Right now I know that the Pop method needs improvement since if T is a value type, returning a nullptr will cause a compiler error.  Also, I'm pretty sure that this can be optimized in a few ways, but I don't see any obvious areas at the moment.  Are there any other specific ways this can be improved?

Any and all feedback is appreciated. 

#ifndef STACK_H
#define STACK_H

template &lt;class T&gt;
class Stack
{
public:
    Stack();
    Stack(T&amp; data);
    ~Stack();

    // Public Stack operations
    void Push(T&amp; data);
    T Pop();
    void Clear();

    // Accessors
    const T&amp; Top() const { return m_top-&gt;m_data; }
    const T&amp; Bottom() const { return m_bottom-&gt;m_data; }
    bool IsEmpty() const { return m_size == 0; }
    int Size() const { return m_size; }

private:
    template &lt;class T&gt; struct StackNode
    {
        StackNode(T&amp; data)
        {
            m_data = data;
            m_nextNode = nullptr;
        }

        T m_data;
        StackNode&lt;T&gt;* m_nextNode;
    };

    int m_size;
    StackNode&lt;T&gt;* m_top;
    StackNode&lt;T&gt;* m_bottom;
};

template &lt;class T&gt; Stack&lt;T&gt;::Stack()
{
    m_top = nullptr;
    m_bottom = nullptr;
    m_size = 0;
}

template &lt;class T&gt; Stack&lt;T&gt;::Stack(T&amp; data)
{
    m_top = m_bottom = new StackNode&lt;T&gt;(data);
    m_size = 1;
}

template &lt;class T&gt; Stack&lt;T&gt;::~Stack()
{
    Clear();
}

template &lt;class T&gt; void Stack&lt;T&gt;::Push(T&amp; data)
{
    StackNode&lt;T&gt;* newNode = new StackNode&lt;T&gt;(data);
    newNode-&gt;m_nextNode = m_top;
    m_top = newNode;
    ++m_size;
}

template &lt;class T&gt; T Stack&lt;T&gt;::Pop()
{
    if (!IsEmpty())
    {
        T returnData = m_top-&gt;m_data;

        StackNode&lt;T&gt;* temp = m_top-&gt;m_nextNode;
        delete m_top;
        m_top = temp;
        --m_size;
        return returnData;
    }

    // there is an issue here because if T is a value type, this will cause an error
    //return nullptr;
}

template &lt;class T&gt; void Stack&lt;T&gt;::Clear()
{
    if (!IsEmpty())
    {
        StackNode&lt;T&gt;* next = nullptr;
        do
        {
            next = m_top-&gt;m_nextNode;
            delete m_top;
            m_top = next;
            --m_size;
        } while (m_top != nullptr);

        m_top = m_bottom = nullptr;
    }
}

#endif
The best thing to do if the caller tries to Pop when the stack is empty is to throw an exception. You're right, there's no return value that makes sense.

You have a bug in void Stack&lt;T&gt;::Push(T&amp; data). If the stack is initially empty, the m_top and m_bottom pointers are nullptr, but if an item is pushed onto an empty stack, the m_bottom pointer is never set.

There's no reason for Stack&lt;T&gt;::Clear() to decrement m_size within the loop. Just set it to 0 at any time. (Though, the compiler may be smart enough to notice this)