Skip to content

Instantly share code, notes, and snippets.

@etam
Last active November 6, 2015 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etam/a846076c540d86084609 to your computer and use it in GitHub Desktop.
Save etam/a846076c540d86084609 to your computer and use it in GitHub Desktop.
functional style stdex::optional to std::vector conversion
#include <experimental/optional>
#include <experimental/type_traits>
#include <type_traits>
#include <utility>
#include <vector>
namespace stdex = std::experimental;
template <
typename Optional,
typename = std::enable_if_t<
stdex::is_same_v<
std::decay_t<Optional>,
stdex::optional<typename std::decay_t<Optional>::value_type>
>
>
>
std::vector<typename std::decay_t<Optional>::value_type> toVector(Optional&& optional)
{
typedef typename std::decay_t<Optional>::value_type value_type;
typedef std::conditional_t<
stdex::is_lvalue_reference_v<Optional>,
std::add_lvalue_reference_t<value_type>,
value_type
> value_type_forward;
auto result = std::vector<value_type>{};
if (optional)
result.push_back(std::forward<value_type_forward>(*optional));
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment