C++ Optional Implementation I haven't done much programming in C++, so I figured I'd try making an Optional class. I'm mainly concerned that I didn't follow good conventions or that I didn't use exceptions well. I know that there are no comments, but for now, let's ignore that.

The idea is that my Optional class can be used instead of pointers and nullptr when I need to return an optional value.

optional.h

#pragma once

namespace extra { // I'm putting random library classes/functions in this namespace
    template &lt;typename T&gt;
    class Optional
    {
    public:
        const static Optional&lt;T&gt; empty;
        static Optional&lt;T&gt; of(const T &amp;v);
        static Optional&lt;T&gt; of(T *v);
        T get() const;
        bool hasValue() const;
        ~Optional() { if (value != nullptr) delete value; }
    private:
        Optional(T *v) : value{ v } {}
        const T *value;
    };

    template&lt;typename T&gt;
    const Optional&lt;T&gt; Optional&lt;T&gt;::empty = Optional::of(nullptr);

#include "optional.impl"
}


optional.impl

#include &lt;stdexcept&gt;

template&lt;typename T&gt;
Optional&lt;T&gt; Optional&lt;T&gt;::of(const T &amp;t) {
    return Optional&lt;T&gt;{ new T(t) };
}

template&lt;typename T&gt;
Optional&lt;T&gt; Optional&lt;T&gt;::of(T *t) {
    if (t != nullptr) return Optional&lt;T&gt;{ new T(*t) };
    return Optional&lt;T&gt;{ nullptr };
}

template&lt;typename T&gt;
bool Optional&lt;T&gt;::hasValue() const {
    return value != nullptr;
}

template&lt;typename T&gt;
T Optional&lt;T&gt;::get() const {
    if (!hasValue()) throw std::runtime_error("Tried to get the value from an optional that has no value");
    return *value;
}

template&lt;typename T&gt;
bool operator==(const Optional&lt;T&gt;&amp; t1, const Optional&lt;T&gt;&amp; t2) {
    if (!t1.hasValue() &amp;&amp; !t2.hasValue()) return true;
    else if (!t1.hasValue() || !t2.hasValue()) return false;
    return t1.get() == t2.get();
}

template&lt;typename T&gt;
bool operator!=(const Optional&lt;T&gt;&amp; t1, const Optional&lt;T&gt;&amp; t2) {
    return !(t1 == t2);
}
Rule of Three.

int main()
{
    extra::Optional&lt;int&gt; x = Optional&lt;int&gt;of(new int(5));
    extra::Optional&lt;int&gt; y(x);   // This still compilers.

    // But your problems don't appear until here
    // at the end of scope.
}


Design

Why do you force the construction of optional objects via the static methods?

    static Optional&lt;T&gt; of(const T &amp;v);
    static Optional&lt;T&gt; of(T *v);


Why not allow people to just construct the objects in the normal way. It would seem more  natural to create objects of this type just like any other object!

Move semantics

You may not want copy semantics (since you are basically implementing a smart pointer. But you could do with implementing move semantics on your object.

Overall

I don't see the need.

If you want to return a pointer (but make sure it is managed). Then pass back a std::unique_ptr&lt;T&gt;.

If you are doing this as a learning exercise that's different. I'll give it a good going over once you have fixed the problems around the rule of three. But that is such a fundamental problem there is no point in reviewing this code further (Though in future version I would like a justification for the static of() methods and examples of why they are needed).