This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 1/3] C++20 constexpr lib part 1/3


On 7/2/19 8:11 AM, Jonathan Wakely wrote:
One more comment. In <bits/stl_algobase.h> this:

+#if __cplusplus > 201703L \
+?????? && defined(_GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED)
+?????????? if (__builtin_is_constant_evaluated())

can be simplified to just:

#ifdef __cpp_lib_is_constant_evaluated
???????? if (std::is_constant_evaluated())

The feature test macro is exactly the check we want here

This is done and the test cases for the non-modifying algorithms are done.

I'm was stumped on the modifying algos though.?? Consider std::copy:

-------------------------------------

constexpr bool
test()
{
?? constexpr std::array<int, 12> ca0{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
?? std::array<int, 12> ma0{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

?? constexpr auto out6 = std::copy(ca0.begin(), ca0.begin() + 8, ma0.begin() + 2);

?? return out6 == ma0.begin() + 10;
}

static_assert(test());
-------------------------------------

This is essentially the same as the Boost test case referenced in p0202.

I've also taken the arrays out as globals with the same result either way:

-------------------------------------

ed@bad-horse:~/cxx_constexpr_lib$ ../bin_constexpr_lib/bin/g++ -std=gnu++2a -c testsuite/25_algorithms/copy/constexpr.cc testsuite/25_algorithms/copy/constexpr.cc: In function ???constexpr bool test()???: testsuite/25_algorithms/copy/constexpr.cc:36:34:???? in ???constexpr??? expansion of ???std::copy<const int*, int*>(ca0.std::array<int, 12>::begin(), (ca0.std::array<int, 12>::begin() + 32), (ma0.std::array<int, 12>::begin() + 8))??? /home/ed/bin_constexpr_lib/include/c++/10.0.0/bits/stl_algobase.h:535:7: in ???constexpr??? expansion of ???std::__copy_move_a2<false, const int*, int*>(std::__miter_base<const int*>(__first), std::__miter_base<const int*>(__last), __result)??? /home/ed/bin_constexpr_lib/include/c++/10.0.0/bits/stl_algobase.h:501:30: in ???constexpr??? expansion of ???std::__copy_move_a<false, const int*, int*>(std::__niter_base<const int*>(__first), std::__niter_base<const int*>(__last), std::__niter_base<int*>(__result))??? /home/ed/bin_constexpr_lib/include/c++/10.0.0/bits/stl_algobase.h:463:30: in ???constexpr??? expansion of ???std::__copy_move<false, true, std::random_access_iterator_tag>::__copy_m<int>(__first, __last, __result)??? /home/ed/bin_constexpr_lib/include/c++/10.0.0/bits/stl_algobase.h:445:24: in ???constexpr??? expansion of ???std::__memmove<false, int>(__result, __first, ((std::size_t)((std::ptrdiff_t)_Num)))??? testsuite/25_algorithms/copy/constexpr.cc:36:80: error: modification of ???ma0??? is not a constant expression ???? 36 |???? constexpr auto out6 = std::copy(ca0.begin(), ca0.begin() + 8, ma0.begin() + 2);
| ^
testsuite/25_algorithms/copy/constexpr.cc: At global scope:
testsuite/25_algorithms/copy/constexpr.cc:41:19: error: non-constant condition for static assertion
???? 41 | static_assert(test());
?????????? |???????????????????????????? ~~~~^~

-------------------------------------

By my reckoning, you have a constexpr source array, an output array that is initialized as it must be for constexpr.?? You have to have a deterministic result after the copy.?? In the local array version the actual iterator is not leaking out - just the results of a calculation that must return one result.

The only thing that 'helps' is removing the constexpr from the iterator return and of course that's the whole point of the thing.

Blargh!

So, you can't modify a constexpr sequence. No actual surprise.?? The returned iterators into non-constexpr sequences can never be literals.?? What you *can* do is make the modifying algorithms so you can make pure functions with them.?? In this connection my previous testcases for non-modifying?? were correct, just not complete because there the returned iterators into constexpr sequences can be (must be) literals.

So here is a new patch for chunk 1 of constexpr library.

Tested with default settings and with -std=gnu++2a on x86_64-linux.

OK?

Ed


Attachment: CL_constexpr_lib
Description: Text document

Attachment: patch_constexpr_lib_2.bz2
Description: application/bzip


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]