Stack implementation using linked list in C++ I'd like some feedback for the code I wrote. I haven't programmed in C++ for two years.

#include &lt;string&gt;

#pragma once
template &lt;class T&gt;
class StackLinkedList
{
public:
    StackLinkedList() : m_List(nullptr), m_Size(0) {}
    ~StackLinkedList();
    void push(const T&amp; val);
    void pop();
    T&amp; top();
    const T&amp; top() const;
    size_t size() const;
    bool empty() const;
private:
    struct Node;
    Node *m_List;
    size_t m_Size;
};

template &lt;class T&gt;
struct StackLinkedList&lt;T&gt;::Node
{
    T m_Val;
    Node* m_Next;
    Node(const T&amp; val, Node* nextPtr) : m_Val(val), m_Next(nextPtr) {}
};

template&lt;class T&gt;
StackLinkedList&lt;T&gt;::~StackLinkedList()
{
    typename Node* nextNodePtr;
    for (typename Node* curNodePtr = m_List; curNodePtr != nullptr; )
    {
        nextNodePtr = curNodePtr-&gt;m_Next;
        delete curNodePtr;
        curNodePtr = nextNodePtr;
    }
}

template&lt;class T&gt;
void StackLinkedList&lt;T&gt;::push(const T&amp; val)
{
    typename Node* newNode = new Node(val, m_List);
    m_List = newNode;
    m_Size++;
}

template&lt;class T&gt;
void StackLinkedList&lt;T&gt;::pop()
{
    if (m_List != nullptr)
    {
        typename Node* curNodePtr = m_List;
        m_List = m_List-&gt;m_Next;
        delete curNodePtr;
        m_Size--;
    }
}

template&lt;class T&gt;
T&amp; StackLinkedList&lt;T&gt;::top()
{
    if (empty())
    {
        throw new std::string("Stack is empty");
    }
    return m_List-&gt;m_Val;
}

template&lt;class T&gt;
const T&amp; StackLinkedList&lt;T&gt;::top() const
{
    if (empty())
    {
        throw new std::string("Stack is empty")
    }
    return m_List-&gt;m_Val;
}

template&lt;class T&gt;
size_t StackLinkedList&lt;T&gt;::size() const
{
    return m_Size;
}

template&lt;class T&gt;
bool StackLinkedList&lt;T&gt;::empty() const
{
    return m_Size == 0;
}
This is quite good, actually. I have one big comment, and a few relatively minor comments. 

RULE OF THREE. Anytime you define a destructor, you want to make sure you define your copy constructor and copy assignment operators. The reason for that is the following:

StackLinkedList&lt;T&gt; stack;
stack.push(...);
stack.push(...);
{
    StackLinkedList&lt;T&gt; stack2 = stack;
    stack2.push(...);
    // &lt;== stack2 deleted here
}
// &lt;== uh-oh, stack2 deleted all the nodes, but stack still thinks
// it has valid objects that it's holding onto that it needs to delete too!


Your class is sorely missing:

StackLinkedList(const StackLinkedList&amp; );

friend void swap(StackLinkedList&amp;, StackLinkedList&amp; );
StackLinkedList&amp; operator=(StackLinkedList other) {
    swap(*this, other);
    return *this;
}


For more on why I suggest this way specifically, see what is the copy-and-swap idiom. 

You didn't mention if you were using C++11 or not. If you are, you want the Rule of Five: you will also need to implement the move constructor and move assignment operator. For this class in particular, those two should be very short. 



Other comments are minor.

Use of typename. There are places where you need to use typename and there are places where you don't. Just don't use it in those latter cases - it's just unnecessary. In your code, literally every usage of typename is redundant and can be omitted. 

top(). The typical implementation of containers is to make operations as fast as possible. So for top(), that would mean not checking empty() - that's up to the user. Just simply return m_List-&gt;m_Val and note that it's just undefined behavior to call top() on an empty stack. If you disagree and want to throw an exception, that is of course perfectly reasonable as well - but this not the way to do it:

throw new std::string("Stack is empty");


You're throwing a std::string*. Anybody that catches it would have to delete it, otherwise you're leaking memory - and std::string* doesn't really convey information. If you look in &lt;stdexcept&gt;, there's a better choice of exception type: std::out_of_range. Standard practice is to throw by value, catch by reference. So that line would be:

throw std::out_of_range("Stack is empty");


#pragma once should be the first line in your file, you have it after a different #include.