Skip to content

Instantly share code, notes, and snippets.

@jwakely
Forked from lichray/make_array.cc
Last active December 24, 2015 23: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 jwakely/6878050 to your computer and use it in GitHub Desktop.
Save jwakely/6878050 to your computer and use it in GitHub Desktop.
#include <array>
template <typename... T>
using common_type_t = typename std::common_type<T...>::type;
template <typename T1, typename... T2>
constexpr auto make_array(T1&& t1, T2&&... t2)
-> std::array<common_type_t<T1, T2...>, sizeof...(T2) + 1>
{
return { std::forward<T1>(t1), std::forward<T2>(t2)... };
}
template <typename T, size_t N, size_t... I>
inline auto _make_array(T const (&arr)[N], std::index_sequence<I...>)
{
return std::array<T, N>{{ arr[I]... }};
}
template <typename T, int N>
inline auto make_array(T const (&arr)[N])
{
return _make_array(arr, std::make_index_sequence<N>());
}
#include <iostream>
int main()
{
auto ch = 'a';
const auto d = 65l;
auto a1 = make_array(ch, d, 0);
std::cout << a1[0] << std::endl;
auto a2 = make_array("abc");
std::cout << a2.data() << std::endl;
std::string s1, s2;
int pos;
std::cin >> pos >> std::ws;
getline(std::cin, s1);
getline(std::cin, s2);
auto a3 = make_array(s1, s2);
std::cout << a3[pos] << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment