mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 22:59:47 +08:00
Added get() non-member template functions.
This commit is contained in:
parent
e74c826d1f
commit
824eb89719
31
array.h
31
array.h
@ -37,6 +37,7 @@ SOFTWARE.
|
||||
#include "exception.h"
|
||||
#include "type_traits.h"
|
||||
#include "parameter_type.h"
|
||||
#include "static_assert.h"
|
||||
|
||||
#ifndef ETL_THROW_EXCEPTIONS
|
||||
#include "error_handler.h"
|
||||
@ -468,6 +469,36 @@ namespace etl
|
||||
rhs.cbegin(),
|
||||
rhs.cend());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets a reference to an element in the array.
|
||||
///\tparam I The index.
|
||||
///\tparam T The type.
|
||||
///\tparam N The array size.
|
||||
///\param a The array.
|
||||
///\return A reference to the element
|
||||
//*************************************************************************
|
||||
template <std::size_t I, typename T, std::size_t N>
|
||||
inline T& get(array<T, N>& a)
|
||||
{
|
||||
STATIC_ASSERT(I < N, "Index out of bounds");
|
||||
return a[I];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets a const reference to an element in the array.
|
||||
///\tparam I The index.
|
||||
///\tparam T The type.
|
||||
///\tparam N The array size.
|
||||
///\param a The array.
|
||||
///\return A const reference to the element
|
||||
//*************************************************************************
|
||||
template <std::size_t I, typename T, std::size_t N>
|
||||
inline const T& get(const array<T, N>& a)
|
||||
{
|
||||
STATIC_ASSERT(I < N, "Index out of bounds");
|
||||
return a[I];
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -333,6 +333,19 @@ namespace
|
||||
CHECK(std::equal(swap_data.begin(), swap_data.end(), data2.begin()));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_get)
|
||||
{
|
||||
Data data1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
const Data data2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
CHECK_EQUAL(data1[3], etl::get<3>(data1));
|
||||
CHECK_EQUAL(data2[3], etl::get<3>(data2));
|
||||
|
||||
// The following line should fail with a compile error.
|
||||
//int i = etl::get<11>(data2);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_equal)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user