Skip to content

Instantly share code, notes, and snippets.

@stevefan1999-personal
Created September 2, 2017 14:09
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 stevefan1999-personal/a5067006cbe7e425f737da5c84b30034 to your computer and use it in GitHub Desktop.
Save stevefan1999-personal/a5067006cbe7e425f737da5c84b30034 to your computer and use it in GitHub Desktop.
(Proof-of-concept) C++17 make_array implementation
template <class T = void, class... Args>
constexpr auto make_array(Args&&... t) {
// T is not void: deduced array type is T
if constexpr (!std::is_same<T, void>()) {
return std::array<T, sizeof...(Args)> { std::forward<Args>(t)... };
}
// T is void: deduced array type is U = std::common_type_t<Args...>
using U = std::common_type_t<Args...>;
// T is void and one of the argument is a specialization of std::reference_wrapper<U>: ill-formed
static_assert(!std::disjunction_v<std::is_same<std::reference_wrapper<U>, Args>...>, "reference_wrappers are not allowed");
return std::array<U, sizeof...(Args)> { std::forward<Args>(t)... };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment