Skip to content

Commit 17ef5aa

Browse files
committed
Restrict initializer list to fundamental types
This commit limits the types that can be used in the initializer list to fundamental types. This change is necessary because when we use the uniform initialization syntax and pass in an array, the compiler incorrectly uses the initialization list constructor instead of the other array constructor.
1 parent 20982df commit 17ef5aa

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

include/af/array.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,9 @@ namespace af
522522
#if AF_API_VERSION >= 38
523523
#if AF_COMPILER_CXX_GENERALIZED_INITIALIZERS
524524
/// \brief Initializer list constructor
525-
template <typename T> array(std::initializer_list<T> list)
525+
template <typename T, typename = typename std::enable_if<
526+
std::is_fundamental<T>::value, void>::type>
527+
array(std::initializer_list<T> list)
526528
: arr(nullptr) {
527529
dim_t size = list.size();
528530
if (af_err __aferr = af_create_array(&arr, list.begin(), 1, &size,
@@ -537,7 +539,8 @@ namespace af
537539
}
538540

539541
/// \brief Initializer list constructor
540-
template <typename T>
542+
template <typename T, typename = typename std::enable_if<
543+
std::is_fundamental<T>::value, void>::type>
541544
array(const af::dim4 &dims, std::initializer_list<T> list)
542545
: arr(nullptr) {
543546
const dim_t *size = dims.get();

test/array.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,25 @@ TEST(Array, ReferenceCount2) {
640640
ASSERT_REF(d, 0) << "After d = c;";
641641
}
642642
}
643+
644+
// This tests situations where the compiler incorrectly assumes the initializer
645+
// list constructor instead of the regular constructor when using the uniform
646+
// initilization syntax
647+
TEST(Array, InitializerListFixAFArray) {
648+
array a = randu(1);
649+
array b{a};
650+
651+
ASSERT_ARRAYS_EQ(a, b);
652+
}
653+
654+
// This tests situations where the compiler incorrectly assumes the initializer
655+
// list constructor instead of the regular constructor when using the uniform
656+
// initilization syntax
657+
TEST(Array, InitializerListFixDim4) {
658+
array a = randu(1);
659+
vector<float> data = {
660+
3.14f, 3.14f, 3.14f, 3.14f, 3.14f, 3.14f, 3.14f, 3.14f, 3.14f
661+
};
662+
array b{dim4(3, 3), data.data()};
663+
ASSERT_ARRAYS_EQ(constant(3.14, 3, 3), b);
664+
}

0 commit comments

Comments
 (0)