Skip to content

Commit 1c3e605

Browse files
authored
Merge pull request InsightSoftwareConsortium#5240 from N-Dekker/itk-Array-CTAD
ENH: Let `itk::Array` support class template argument deduction (CTAD)
2 parents dc76ed8 + 38ae4b0 commit 1c3e605

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

Modules/Core/Common/include/itkArray.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,25 @@ class ITK_TEMPLATE_EXPORT Array : public vnl_vector<TValue>
7878
* the array does not manage the memory of the buffer. It merely points to
7979
* that location and it is the user's responsibility to delete it.
8080
* If "LetArrayManageMemory" is true, then this class will free the
81-
* memory when this object is destroyed. */
82-
Array(ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
81+
* memory when this object is destroyed.
82+
* \note This constructor supports class template argument deduction (CTAD). */
83+
Array(TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
8384

8485
#if defined(ITK_LEGACY_REMOVE)
8586
/** Constructor that initializes array with contents from a user supplied
8687
* const buffer. The pointer to the buffer and the length is specified. By default,
8788
* the array does a deep copy of the const pointer data, so the array class also
88-
* manages memory. */
89-
Array(const ValueType * datain, SizeValueType sz);
89+
* manages memory.
90+
* \note This constructor supports class template argument deduction (CTAD). */
91+
Array(const TValue * datain, SizeValueType sz);
9092

9193
#else // defined ( ITK_LEGACY_REMOVE )
9294
/** Constructor that initializes array with contents from a user supplied
9395
* buffer. The pointer to the buffer and the length is specified. The array
9496
* does a deep copy of the const pointer data, so the array class also
95-
* manages memory. The 3rd argument is only for backward compatibility. */
96-
Array(const ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
97+
* manages memory. The 3rd argument is only for backward compatibility.
98+
* \note This constructor supports class template argument deduction (CTAD). */
99+
Array(const TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
97100
#endif
98101

99102
/** Constructor to initialize an array from another of any data type */
@@ -208,6 +211,10 @@ class ITK_TEMPLATE_EXPORT Array : public vnl_vector<TValue>
208211
bool m_LetArrayManageMemory{ true };
209212
};
210213

214+
// Deduction guide to avoid compiler warnings (-wctad-maybe-unsupported) when using class template argument deduction.
215+
template <typename TValue>
216+
Array(TValue *, typename vnl_vector<TValue>::size_type, bool) -> Array<TValue>;
217+
211218
template <typename TValue>
212219
std::ostream &
213220
operator<<(std::ostream & os, const Array<TValue> & arr)

Modules/Core/Common/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@ if(ITK_BUILD_SHARED_LIBS AND ITK_DYNAMIC_LOADING)
17141714
endif()
17151715

17161716
set(ITKCommonGTests
1717+
itkArrayGTest.cxx
17171718
itkArray2DGTest.cxx
17181719
itkAggregateTypesGTest.cxx
17191720
itkBitCastGTest.cxx
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
19+
// First include the header file to be tested:
20+
#include "itkArray.h"
21+
#include <gtest/gtest.h>
22+
23+
namespace
24+
{
25+
// Checks that `itk::Array` supports class template argument deduction (CTAD).
26+
template <typename TValue>
27+
constexpr bool
28+
CheckClassTemplateArgumentDeduction()
29+
{
30+
using ExpectedType = itk::Array<TValue>;
31+
32+
const TValue constValue{};
33+
static_assert(std::is_same_v<decltype(itk::Array(&constValue, 1)), ExpectedType>,
34+
"The `Array(const ValueType *, ...)` constructor should support CTAD!");
35+
36+
TValue nonConstValue{};
37+
static_assert(std::is_same_v<decltype(itk::Array(&nonConstValue, 1)), ExpectedType>,
38+
"The `Array(ValueType *, ...)` constructor should support CTAD!");
39+
return true;
40+
}
41+
} // namespace
42+
43+
44+
static_assert(CheckClassTemplateArgumentDeduction<int>() && CheckClassTemplateArgumentDeduction<float>());

0 commit comments

Comments
 (0)