public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98933] New: P0784R7 example support: constexpr new
@ 2021-02-02 14:16 markus.kuehni at triviso dot ch
  2021-02-02 14:23 ` [Bug c++/98933] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: markus.kuehni at triviso dot ch @ 2021-02-02 14:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98933

            Bug ID: 98933
           Summary: P0784R7 example support: constexpr new
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: markus.kuehni at triviso dot ch
  Target Milestone: ---

Trying to recreate the
[P0784R7](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r7.html)
code example. 


<source>:13:10: error: 'mark_immutable_if_constexpr' is not a member of 'std'
   13 |     std::mark_immutable_if_constexpr(this->ps);


Removed std::mark_immutable_if_constexpr(this->ps);


<source>:22:31: error: no matching function for call to 'S<char>::S(const char
[7])'
   22 | constexpr S<char> str("Hello!");
      |                               ^


Added std::add_const<T>::type for constructor argument p. 


<source>:22:31:   in 'constexpr' expansion of 'S<char>("Hello!")'
<source>:11:7: error: call to non-'constexpr' function 'void* operator
new(std::size_t, void*)'
   11 |       new(this->ps+k) T{p[k]};


Replaced with std::construct_at() and std::destroy_at() for symmetry, see
[91369](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91369). 


In file included from
/opt/compiler-explorer/gcc-trunk-20210202/include/c++/11.0.0/memory:64,
                 from <source>:1:
/opt/compiler-explorer/gcc-trunk-20210202/include/c++/11.0.0/bits/allocator.h:171:50:
error: 'S<char>("Hello!")' is not a constant expression because it refers to a
result of 'operator new'
  171 |           return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));


Now I'm stuck. 

A cleaned-up version is here:

https://godbolt.org/z/KWMqna

```
#include <memory>
#include <new>

template<typename T> struct S : std::allocator<T>  {
        std::size_t sz;
        T *ps;

    template<std::size_t N> 
    constexpr S(typename std::add_const<T>::type (&p)[N]) :
                        sz { N }, ps {this->allocate(N)} {
                for (std::size_t k = 0; k<N; ++k) {
                        std::construct_at(ps+k, p[k]);
                }
        }
        constexpr ~S() {
                for (std::size_t k = 0; k < this->sz; ++k) {
                        std::destroy_at(ps+k);
                }
                this->deallocate(this->ps, this->sz);
        }
};

constexpr S<char> str("Hello!");
// str ends up pointing to a static array
// containing the string "Hello!".

```

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/98933] P0784R7 example support: constexpr new
  2021-02-02 14:16 [Bug c++/98933] New: P0784R7 example support: constexpr new markus.kuehni at triviso dot ch
@ 2021-02-02 14:23 ` jakub at gcc dot gnu.org
  2021-02-02 14:32 ` markus.kuehni at triviso dot ch
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-02-02 14:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98933

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Have you noticed the
In Kona 2019, this approach was considered too brittle, and as a result
non-transient allocation was removed from the feature set. 
line just below that example?
This is not valid C++20.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/98933] P0784R7 example support: constexpr new
  2021-02-02 14:16 [Bug c++/98933] New: P0784R7 example support: constexpr new markus.kuehni at triviso dot ch
  2021-02-02 14:23 ` [Bug c++/98933] " jakub at gcc dot gnu.org
@ 2021-02-02 14:32 ` markus.kuehni at triviso dot ch
  2021-02-02 14:37 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: markus.kuehni at triviso dot ch @ 2021-02-02 14:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98933

--- Comment #2 from Mark <markus.kuehni at triviso dot ch> ---
(In reply to Jakub Jelinek from comment #1)
> Have you noticed the
> In Kona 2019, this approach was considered too brittle, and as a result
> non-transient allocation was removed from the feature set. 
> line just below that example?
> This is not valid C++20.

So just to understand this right. It is ok as long as it does not survive
compilation?

But how are you going to implement constexpr std::vector and std::string then? 

(I'm actually somehow trying to bridge the time until that happens ;-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/98933] P0784R7 example support: constexpr new
  2021-02-02 14:16 [Bug c++/98933] New: P0784R7 example support: constexpr new markus.kuehni at triviso dot ch
  2021-02-02 14:23 ` [Bug c++/98933] " jakub at gcc dot gnu.org
  2021-02-02 14:32 ` markus.kuehni at triviso dot ch
@ 2021-02-02 14:37 ` jakub at gcc dot gnu.org
  2021-02-02 14:43 ` markus.kuehni at triviso dot ch
  2021-02-03 16:17 ` markus.kuehni at triviso dot ch
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-02-02 14:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98933

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
You just need to allocate and deallocate during the same constexpr evaluation.
So doing
constexpr int
foo ()
{
  ... std::construct_at(...);
  ...
  ... std::destroy_at(...);
  ...
  return ...;
}
is fine, but not what you were trying to do, which is turn the transient
allocations into something permanent.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/98933] P0784R7 example support: constexpr new
  2021-02-02 14:16 [Bug c++/98933] New: P0784R7 example support: constexpr new markus.kuehni at triviso dot ch
                   ` (2 preceding siblings ...)
  2021-02-02 14:37 ` jakub at gcc dot gnu.org
@ 2021-02-02 14:43 ` markus.kuehni at triviso dot ch
  2021-02-03 16:17 ` markus.kuehni at triviso dot ch
  4 siblings, 0 replies; 6+ messages in thread
From: markus.kuehni at triviso dot ch @ 2021-02-02 14:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98933

--- Comment #4 from Mark <markus.kuehni at triviso dot ch> ---
So this is the only way?

```
#include <memory>

template<typename T> struct S  {
        std::size_t sz;
        T *ps;

    template<std::size_t N> 
    constexpr S(T (&p)[N]) :
                        sz { N }, ps {p} {}
};

static constexpr char buf[] {"Hello"};
constexpr S str(buf);
// str ends up pointing to a static array
// containing the string "Hello!".
```

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/98933] P0784R7 example support: constexpr new
  2021-02-02 14:16 [Bug c++/98933] New: P0784R7 example support: constexpr new markus.kuehni at triviso dot ch
                   ` (3 preceding siblings ...)
  2021-02-02 14:43 ` markus.kuehni at triviso dot ch
@ 2021-02-03 16:17 ` markus.kuehni at triviso dot ch
  4 siblings, 0 replies; 6+ messages in thread
From: markus.kuehni at triviso dot ch @ 2021-02-03 16:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98933

Mark <markus.kuehni at triviso dot ch> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #5 from Mark <markus.kuehni at triviso dot ch> ---
This is obviously not a bug. 

And Bugzilla is not a learning tool.

(though the experts seem to be here ;-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-02-03 16:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-02 14:16 [Bug c++/98933] New: P0784R7 example support: constexpr new markus.kuehni at triviso dot ch
2021-02-02 14:23 ` [Bug c++/98933] " jakub at gcc dot gnu.org
2021-02-02 14:32 ` markus.kuehni at triviso dot ch
2021-02-02 14:37 ` jakub at gcc dot gnu.org
2021-02-02 14:43 ` markus.kuehni at triviso dot ch
2021-02-03 16:17 ` markus.kuehni at triviso dot ch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).