Skip to content

Commit

Permalink
Fix unique_ptr<T>::operator[] for GCC 9 and 10
Browse files Browse the repository at this point in the history
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 <op-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins Combined Simics CI <combined-simics-ci+hostboot@us.ibm.com>
Tested-by: FSP CI Jenkins <fsp-CI-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP HW <op-hw-jenkins+hostboot@us.ibm.com>
Tested-by: Hostboot CI <hostboot-ci+hostboot@us.ibm.com>
Reviewed-by: Roland Veloz <rveloz@us.ibm.com>
Reviewed-by: Isaac Salem <isaac.salem@ibm.com>
Reviewed-by: Daniel M Crowell <dcrowell@us.ibm.com>
  • Loading branch information
ibmzach authored and dcrowell77 committed Mar 22, 2022
1 parent fba7dc6 commit ac366f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/include/util/impl/unique_ptr.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2016,2020 */
/* Contributors Listed Below - COPYRIGHT 2016,2022 */
/* [+] International Business Machines Corp. */
/* */
/* */
Expand Down Expand Up @@ -402,12 +402,26 @@ namespace std
* @return Value of T::operator[](i)
*/
template<typename Index, typename X = T>
typename enable_if<is_array<X>::value, decltype(declval<X>()[declval<Index>()])>::type
typename enable_if<is_array<X>::value, decltype(declval<X>()[declval<Index>()])>::type&
operator[](const Index i)
{
return iv_ptr[i];
}

/* @brief Index operator for unique_ptr<T[]>.
*
* operator[] is not callable on a unique_ptr to non-array types.
*
* @param[in] i Index into the array
* @return Value of T::operator[](i)
*/
template<typename Index, typename X = T>
typename enable_if<is_array<X>::value, decltype(declval<X>()[declval<Index>()])>::type&
operator[](const Index i) const
{
return iv_ptr[i];
}

template <typename U, typename D> friend class unique_ptr;

private:
Expand Down
10 changes: 8 additions & 2 deletions src/usr/testcore/lib/unique_ptr.H
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2020 */
/* Contributors Listed Below - COPYRIGHT 2020,2022 */
/* [+] International Business Machines Corp. */
/* */
/* */
Expand Down Expand Up @@ -76,6 +76,9 @@ public:
unique_ptr<practice_struct[]> i(new practice_struct[10] { {1}, {2}, {3}, {4}, {5},
{6}, {7}, {8}, {9}, {10} });
unique_ptr<practice_struct> h;
const unique_ptr<practice_struct[]> m(new practice_struct[1] { });
const unique_ptr<const practice_struct[]> m2(new const practice_struct[1] { });

h = std::move(c);

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

if (practice_struct::instance_count != 14)
if (practice_struct::instance_count != 16)
{
TS_FAIL("testUniquePtr: unique_ptr constructed an incorrect number of objects. "
"Remaining object count is %d",
practice_struct::instance_count);
}

m[0].x = 0; // write through const pointer to mutable data
m2[0].x; // read through const pointer to const data
}

if (practice_struct::instance_count != 0)
Expand Down

0 comments on commit ac366f7

Please sign in to comment.