Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ target_sources(
include
FILES
include/stdx/algorithm.hpp
include/stdx/array.hpp
include/stdx/atomic.hpp
include/stdx/atomic_bitset.hpp
include/stdx/bit.hpp
Expand Down
1 change: 1 addition & 0 deletions docs/intro.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ into headers whose names match the standard.
The following headers are available:

* https://github.com/intel/cpp-std-extensions/blob/main/include/stdx/algorithm.hpp[`algorithm.hpp`]
* https://github.com/intel/cpp-std-extensions/blob/main/include/stdx/array.hpp[`array.hpp`]
* https://github.com/intel/cpp-std-extensions/blob/main/include/stdx/atomic.hpp[`atomic.hpp`]
* https://github.com/intel/cpp-std-extensions/blob/main/include/stdx/atomic_bitset.hpp[`atomic_bitset.hpp`]
* https://github.com/intel/cpp-std-extensions/blob/main/include/stdx/bit.hpp[`bit.hpp`]
Expand Down
42 changes: 42 additions & 0 deletions include/stdx/array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <array>
#include <type_traits>
#include <utility>

namespace stdx {
inline namespace v1 {

template <typename D = void, typename... Ts>
constexpr auto make_array(Ts &&...ts) {
if constexpr (not std::is_void_v<D>) {
return std::array<D, sizeof...(Ts)>{std::forward<Ts>(ts)...};
} else {
using A = std::common_type_t<Ts...>;
return std::array<A, sizeof...(Ts)>{std::forward<Ts>(ts)...};
}
}

template <template <auto> typename D, typename T, T... Is>
constexpr auto make_array(std::integer_sequence<T, Is...>) {
using A = std::common_type_t<decltype(D<Is>::value)...>;
return std::array<A, sizeof...(Is)>{D<Is>::value...};
}

template <template <auto> typename D, auto N> constexpr auto make_array() {
return make_array<D>(std::make_integer_sequence<decltype(N), N>{});
}

template <typename T, T... Is, typename F>
constexpr auto make_array(std::integer_sequence<T, Is...>, F &&f) {
using A = std::common_type_t<decltype(f.template operator()<Is>())...>;
return std::array<A, sizeof...(Is)>{f.template operator()<Is>()...};
}

template <auto N, typename F> constexpr auto make_array(F &&f) {
return make_array(std::make_integer_sequence<decltype(N), N>{},
std::forward<F>(f));
}

} // namespace v1
} // namespace stdx
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_tests(
FILES
algorithm
always_false
array
atomic
atomic_override
atomic_bitset
Expand Down
45 changes: 45 additions & 0 deletions test/array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdx/array.hpp>

#include <catch2/catch_test_macros.hpp>

#include <array>

TEST_CASE("make_array (variadic arguments)", "[array]") {
auto arr = stdx::make_array(1, 2, 3, 4, 5);
STATIC_CHECK(std::is_same_v<decltype(arr), std::array<int, 5>>);
CHECK(arr == std::array{1, 2, 3, 4, 5});
}

namespace {
template <auto I> using V = std::integral_constant<decltype(I), I + 1>;
}

TEST_CASE("make_array by sequence (template with ::value)", "[array]") {
auto arr = stdx::make_array<V>(std::make_integer_sequence<int, 5>{});
STATIC_CHECK(std::is_same_v<decltype(arr), std::array<int, 5>>);
CHECK(arr == std::array{1, 2, 3, 4, 5});
}

TEST_CASE("make_array by extent (template with ::value)", "[array]") {
auto arr = stdx::make_array<V, 5>();
STATIC_CHECK(std::is_same_v<decltype(arr), std::array<int, 5>>);
CHECK(arr == std::array{1, 2, 3, 4, 5});
}

namespace {
struct {
template <auto I> auto operator()() { return I + 1; }
} f;
} // namespace

TEST_CASE("make_array by sequence (factory function template)", "[array]") {
auto arr = stdx::make_array(std::make_integer_sequence<int, 5>{}, f);
STATIC_CHECK(std::is_same_v<decltype(arr), std::array<int, 5>>);
CHECK(arr == std::array{1, 2, 3, 4, 5});
}

TEST_CASE("make_array by extent (factory function template)", "[array]") {
auto arr = stdx::make_array<5>(f);
STATIC_CHECK(std::is_same_v<decltype(arr), std::array<int, 5>>);
CHECK(arr == std::array{1, 2, 3, 4, 5});
}