Skip to content

Commit ac366f7

Browse files
ibmzachdcrowell77
authored andcommitted
Fix unique_ptr<T>::operator[] for GCC 9 and 10
This commit makes a small change to fix operator[] for the unique_ptr class for GCC 9 and 10. On GCC 9 and 10, operator[] was not returning a reference and our testcases were failing to compile, even though GCC 8 does compile it to return a reference. Also adds a testcase for const unique_ptr and unique_ptr-to-const. Resolves #204 Change-Id: Ia8bca57fd70656446eb81f8bcca4122cb4d280ff Reviewed-on: http://rchgit01.rchland.ibm.com/gerrit1/128882 Tested-by: Jenkins OP Build CI <[email protected]> Tested-by: Jenkins Server <[email protected]> Tested-by: Jenkins Combined Simics CI <[email protected]> Tested-by: FSP CI Jenkins <[email protected]> Tested-by: Jenkins OP HW <[email protected]> Tested-by: Hostboot CI <[email protected]> Reviewed-by: Roland Veloz <[email protected]> Reviewed-by: Isaac Salem <[email protected]> Reviewed-by: Daniel M Crowell <[email protected]>
1 parent fba7dc6 commit ac366f7

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/include/util/impl/unique_ptr.H

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* OpenPOWER HostBoot Project */
77
/* */
8-
/* Contributors Listed Below - COPYRIGHT 2016,2020 */
8+
/* Contributors Listed Below - COPYRIGHT 2016,2022 */
99
/* [+] International Business Machines Corp. */
1010
/* */
1111
/* */
@@ -402,12 +402,26 @@ namespace std
402402
* @return Value of T::operator[](i)
403403
*/
404404
template<typename Index, typename X = T>
405-
typename enable_if<is_array<X>::value, decltype(declval<X>()[declval<Index>()])>::type
405+
typename enable_if<is_array<X>::value, decltype(declval<X>()[declval<Index>()])>::type&
406406
operator[](const Index i)
407407
{
408408
return iv_ptr[i];
409409
}
410410

411+
/* @brief Index operator for unique_ptr<T[]>.
412+
*
413+
* operator[] is not callable on a unique_ptr to non-array types.
414+
*
415+
* @param[in] i Index into the array
416+
* @return Value of T::operator[](i)
417+
*/
418+
template<typename Index, typename X = T>
419+
typename enable_if<is_array<X>::value, decltype(declval<X>()[declval<Index>()])>::type&
420+
operator[](const Index i) const
421+
{
422+
return iv_ptr[i];
423+
}
424+
411425
template <typename U, typename D> friend class unique_ptr;
412426

413427
private:

src/usr/testcore/lib/unique_ptr.H

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* OpenPOWER HostBoot Project */
77
/* */
8-
/* Contributors Listed Below - COPYRIGHT 2020 */
8+
/* Contributors Listed Below - COPYRIGHT 2020,2022 */
99
/* [+] International Business Machines Corp. */
1010
/* */
1111
/* */
@@ -76,6 +76,9 @@ public:
7676
unique_ptr<practice_struct[]> i(new practice_struct[10] { {1}, {2}, {3}, {4}, {5},
7777
{6}, {7}, {8}, {9}, {10} });
7878
unique_ptr<practice_struct> h;
79+
const unique_ptr<practice_struct[]> m(new practice_struct[1] { });
80+
const unique_ptr<const practice_struct[]> m2(new const practice_struct[1] { });
81+
7982
h = std::move(c);
8083

8184
// Try custom deleter with function pointer
@@ -123,12 +126,15 @@ public:
123126
TS_FAIL("Array access through unique_ptr returned incorrect value (expected 4)");
124127
}
125128

126-
if (practice_struct::instance_count != 14)
129+
if (practice_struct::instance_count != 16)
127130
{
128131
TS_FAIL("testUniquePtr: unique_ptr constructed an incorrect number of objects. "
129132
"Remaining object count is %d",
130133
practice_struct::instance_count);
131134
}
135+
136+
m[0].x = 0; // write through const pointer to mutable data
137+
m2[0].x; // read through const pointer to const data
132138
}
133139

134140
if (practice_struct::instance_count != 0)

0 commit comments

Comments
 (0)