Skip to content

Commit

Permalink
Teuchos: Array class now supports initializer_list construction
Browse files Browse the repository at this point in the history
  • Loading branch information
rppawlo committed Oct 17, 2023
1 parent 7259ebc commit 19ce13f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/teuchos/core/src/Teuchos_Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ class Array
template<int N>
inline Array(const Tuple<T,N>& t);

//! Create an array with braced initialization.
inline Array(std::initializer_list<T> list);

//! Destructor.
inline ~Array();

Expand Down Expand Up @@ -859,6 +862,10 @@ Array<T>::Array(const Tuple<T,N>& t)
insert(begin(), t.begin(), t.end());
}

template<typename T> inline
Array<T>::Array(std::initializer_list<T> a)
: vec_(rcp(new std::vector<T>{a}))
{}

template<typename T> inline
Array<T>& Array<T>::operator=(const Array& a)
Expand Down
15 changes: 15 additions & 0 deletions packages/teuchos/core/test/MemoryManagement/Array_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,21 @@ bool testArray( const int n, Teuchos::FancyOStream &out )
TEST_COMPARE_ARRAYS( a2, a );
}

{
out << "\nTest constructing Array<T> from initializer_list ...\n";
const Array<T> a2{as<T>(1),as<T>(2),as<T>(3),as<T>(4)};
TEST_EQUALITY( a2.size(), 4);
out << "Test that a2[i] == i+1 ... ";
bool local_success = true;
for (size_t i=0; i < a2.size(); ++i) {
TEST_ARRAY_ELE_EQUALITY( a2, i, as<T>(i+1) );
}
if (local_success)
out << "passed\n";
else
success = false;
}

{
out << "\nTest comparison operators ...\n";
Array<T> a2(a);
Expand Down

0 comments on commit 19ce13f

Please sign in to comment.