diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 8346f40..e3e086d 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -13,6 +13,11 @@ #include "yaml-cpp/node/iterator.h" #include "yaml-cpp/node/node.h" #include +#include + +#ifdef __cpp_lib_optional +#include +#endif namespace YAML { inline Node::Node() : m_isValid(true), m_pMemory(nullptr), m_pNode(nullptr) {} @@ -115,7 +120,8 @@ struct as_if { explicit as_if(const Node& node_) : node(node_) {} const Node& node; - T operator()() const { + template + auto operator()() const -> typename std::tuple_element<1, std::tuple::decode), T>>::type { if (!node.m_pNode) throw TypedBadConversion(node.Mark()); @@ -124,6 +130,19 @@ struct as_if { return t; throw TypedBadConversion(node.Mark()); } + +#ifdef __cpp_lib_optional + template + auto operator()() const -> typename std::tuple_element<1, std::tuple::decode_optional), T>>::type { + if (!node.m_pNode) + throw TypedBadConversion(node.Mark()); + + std::optional t = convert::decode_optional(node); + if (t) + return *t; + throw TypedBadConversion(node.Mark()); + } +#endif }; template <>