Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

std::unexpected constructor contraint

I am reading the cpp 23 standard and I stumbled upon std::unexpected.

Section expected.un.cons defines

template<class Err = E>
constexpr explicit unexpected(Err&& e);

with the following costraint (among others)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

is_same_v<remove_cvref_t<Err>, unexpected> is false; 

When would this expression be true given that unexpected is a class template?

>Solution :

unexpected is the injected-class-name in the scope of the class template. It refers to the current specialization of the template. It does not refer to the template itself as the same name would outside the class scope.

For example if you write

template<typename T>
struct X {
    static X x;
};

then X<T>::x is of type X<T>. You don’t have to repeat the template argument list in the declaration of x. If none is given, then it resolves to the injected-class-name which refers to the current specialization of the template with the same template argument list.

The constraint is there so that the constructor can’t be chosen over the copy constructor in overload resolution, which may otherwise happen for non-const lvalue arguments.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading