Is there a way to encapsulate a type in a template class (akin to something like std::optional) that has all the necessary special constructors and assignment operators (i.e. copy ctor/assignment, move ctor/assignment) but only "enables" them if the underlying type has those functions? The functions in type_traits like std::is_copy_constructible look like they might help but I'm not sure how to use them to achieve this goal. For reference, the type I'm trying to implement is similar to std::optional but instead of the alternate value being simply "none" I want to use a custom error type. E.g.
template <typename T>
class ErrorOr {
public:
enum class Error {
FATAL,
WARNING,
NONE,
};
ErrorOr(T val) : val(val), error(Error::NONE) {}
ErrorOr(Error error) : error(error) {}
// TODO: Implement copy/move ctors/assignment operators that only
// exist if they do for the underlying T
T get() { val; }
private:
T val;
Error error;
};
This is a very bare-bones/minimal implementation that doesn't have a lot of the necessary features but hopefully illustrates the point I'm trying to make.
Is this possible in C++11?
Aucun commentaire:
Enregistrer un commentaire