#include #include #include std::experimental::optional getFirst(const std::vector& vec){ if ( !vec.empty() ) return std::experimental::optional(vec[0]); else return std::experimental::optional(); } int main(){ std::vector myVec{1,2,3}; std::vector myEmptyVec; auto myInt= getFirst(myVec); if (myInt){ std::cout << "*myInt: " << *myInt << std::endl; std::cout << "myInt.value(): " << myInt.value() << std::endl; std::cout << "myInt.value_or(2017):" << myInt.value_or(2017) << std::endl; } std::cout << std::endl; std::experimental::optional myEmptyInt= getFirst(myEmptyVec); if (!myEmptyInt){ std::cout << "myEmptyInt.value_or(2017):" << myEmptyInt.value_or(2017) << std::endl; } }