Skip to content

Commit e61d406

Browse files
committed
Restrict MatrixBase 1 element ctor to 1x1 matrices.
1 parent c68825c commit e61d406

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

‎src/libs/math/math/MatrixBase-impl.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ constexpr MatrixBase<TMA>::MatrixBase(T_element... vaElements) :
1616

1717

1818
template<TMP>
19-
constexpr MatrixBase<TMA>::MatrixBase(T_number aSingleElement) :
19+
constexpr MatrixBase<TMA>::MatrixBase(T_number aSingleElement) requires (N_rows*N_cols == 1) :
2020
mStore{ {aSingleElement} }
2121
{}
2222

‎src/libs/math/math/MatrixBase.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class MatrixBase
134134
{ setZero(); }
135135

136136
// Implementer note:
137-
// I don't feel confident enought with my understanding of aggregate initialization
137+
// I don't feel confident enough with my understanding of aggregate initialization
138138
// (see: https://en.cppreference.com/w/cpp/language/aggregate_initialization)
139139
// to know when it can and cannot throw exceptions. So be conservative and noexcept(false).
140140
//
@@ -157,9 +157,12 @@ class MatrixBase
157157
std::enable_if_t<sizeof...(T_element) == N_rows*N_cols && (N_rows*N_cols > 1), int> = 0>
158158
constexpr MatrixBase(T_element... vaElements) /*noexcept (see note)*/;
159159

160-
// Note: split because of a complication with explicit handling
160+
// Note: split because of a complication with explicit handling of the ctor above (see comments).
161161
// (C++ 20 introduce conditional explicit, which could be used instead)
162-
constexpr explicit MatrixBase(T_number aSingleElement) /*noexcept (see note)*/;
162+
// This constructor is restricted to the case where the matrix is 1 element exactly.
163+
// Note: Without `requires` clause, one could rely on SFINAE, by templating the method with
164+
// <typename = std::enable_if_t<(N_rows*N_cols == 1)>>
165+
constexpr explicit MatrixBase(T_number aSingleElement) requires (N_rows*N_cols == 1) /*noexcept (see note)*/;
163166

164167
/// \brief Explicit cast to another derived type of same dimensions and scalar type
165168
template <class T_otherDerived,

0 commit comments

Comments
 (0)