I'm currently trying to wrap my head around monads. Unfortunately, most articles on the topic use Haskell without properly explaining the notation. Yet, as I am mainly programming in C++, I would like understand monads without learning a new programming language...
From what I gathered on the web, a monad M
is a type constructor for a type T
, which provides at least the following operations:
- an actual way to construct the type
T
- a converter for converting an arbitrary type to
T
(apparently called return in Haskell) - a combinator for applying the value stored in
T
to a functionf
(apparently called bind in Haskell)
Applying these criteria to C++, it seems to me that std::unique_ptr
could be considered a monad. Is this true?
My reasoning is as follows:
The std::unique_ptr
template is used to construct the actual type std::unique_ptr<T>
, thus:
- the type constructor is either
std::unique_ptr<T>{}
orstd::make_unique<T>()
- the converter, again, would be the constructor or
std::make_unique
(with arguments...) - the combinator would be either
std::bind(func, pointer.get())
,std::bind(func, *pointer)
or an equivalent lambda
Do you agree, or does the call to operator*()
/.get()
for the combinator disqualify std::unique_ptr
from being a monad?
I get, that using std::unique_ptr
as a monad might not make sense because it carries owner semantic. I would just like to know, if it is one.