Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88975

Re: Found a use case for `const` by value return, a shared queue

From "Alf P. Steinbach" <alf.p.steinbach@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Found a use case for `const` by value return, a shared queue
Date 2023-02-05 15:37 +0100
Organization A noiseless patient Spider
Message-ID <troevp$2hphi$1@dont-email.me> (permalink)
References <trmist$24rbq$1@dont-email.me>

Show all headers | View raw


On 2023-02-04 10:32 PM, Alf P. Steinbach wrote:
> I finally encountered a use for `const T` as function return type.
> 
> This code appears to work well for the single purpose it's been used 
> for, but it's not tested or even used extensively. And that includes 
> that I haven't even tested/used that `const T` return function yet. But:
> 
> 
>      template< class Item >
>      class Shared_queue_
>      {
>          queue<Item>     m_queue;
> 
>          mutable mutex                   m_mutex;
>          mutable condition_variable      m_condition;
> 
>          auto length() const -> int      { return int_size_of( m_queue ); }
>          auto is_empty() const -> bool   { return m_queue.empty(); }
> 
>      public:
>          class Access:
>              public Movable
>          {
>              friend class Shared_queue_;
> 
>              Shared_queue_*      m_p_queue;
>              Mutex_lock          m_mutex_lock;
> 
>              Access( Shared_queue_& q ):
>                  m_p_queue( &q ),
>                  m_mutex_lock( q.m_mutex )
>              {}
> 
>              Access( const Shared_queue_& q ):
>                  m_p_queue( &q ),
>                  m_mutex_lock( q.m_mutex )
>              {}
> 
>          public:
>              auto length() const -> int      { return m_p_queue->length(); }
>              auto is_empty() const -> bool   { return m_p_queue->is_empty(); }
> 
>              void wait_for_items()
>              {
>                  if( not is_empty() ) { return; }
>                  m_p_queue->m_condition.wait( m_mutex_lock, [&]() -> bool { return not is_empty(); } );
>              }
> 
>              auto wait_for_items_for( const Duration duration )
>                  -> bool
>              {
>                  if( not is_empty() ) { return true; }
>                  // TODO: handle possible overflow in `steady_clock::now() + duration`.
>                  const bool timed_out = not m_p_queue->m_condition.wait_for(
>                      m_mutex_lock, duration, [&]() -> bool { return not is_empty(); }
>                      );
>                  return not timed_out;
>              }
> 
>              void enq( Item item, const bool notify = true )
>              {
>                  m_p_queue->m_queue.push( move( item ) );
>                  if( notify ) { m_p_queue->m_condition.notify_one(); }
>              }
> 
>              auto deq() -> Item
>              {
>                  wait_for_items();
>                  hopefully( not is_empty() )
>                      or FSM_FAIL( "Waited for items but got none; queue is empty." );
>                  return popped_front_of( m_p_queue->m_queue );
>              }
>          };
> 
>          auto access() -> Access                 { return Access( *this ); }
>          auto access() const -> const Access     { return Access( *this ); }
>      };

Right after I went to bed last night it hit me that the `const` `access` 
function couldn't possibly work as written, because the `Access` 
constructor it calls tries to initialize a `Shared_queue_*` with a 
`const Shared_queue_*`.

I haven't yet tried to call it (functions in templates can be very wrong 
so that they wouldn't compile if used, as long as they're not actually 
used), but after two cups of coffee I now think the same.

I saw two possible solutions before I went to sleep:

* use a union for the proxy object's pointer-to-queue, or
* use separate proxy types for the mutable and const cases.

I am not at all sure that type punning `const T*` and `T*` via a union, 
even if it is dynamically safe and correct from a machine code point of 
view, is formally allowed in C++?

With separate proxy types a `Mutable_access_` can inherit from a 
`Const_access_` and have the pointer type or constness thereof as a 
template parameter, but though it's type-wise clean it also feels like a 
kludge; complex. Furthermore the `const` on the return type will then 
not matter. So is it that with formally correct C++ I have not run into 
a case where `const` on a by-value return type is reasonable?


- Alf

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-04 22:32 +0100
  Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-05 15:37 +0100
    Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-05 22:45 +0100
  Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-05 12:49 -0800
    Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-05 22:39 +0100
      Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-06 22:09 +0100
        Re: Found a use case for `const` by value return, a shared queue scott@slp53.sl.home (Scott Lurndal) - 2023-02-06 21:35 +0000
          Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-06 23:44 +0100
            Re: Found a use case for `const` by value return, a shared queue scott@slp53.sl.home (Scott Lurndal) - 2023-02-07 01:34 +0000
            Re: Found a use case for `const` by value return, a shared queue Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-07 07:42 -0800
              Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-07 17:34 +0100
                Re: Found a use case for `const` by value return, a shared queue Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-24 07:51 -0800
                Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-24 18:48 +0100
                Re: Found a use case for `const` by value return, a shared queue Daniel <danielaparker@gmail.com> - 2023-02-26 09:50 -0800
                Re: Found a use case for `const` by value return, a shared queue Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-03-05 08:38 -0800
                Re: Found a use case for `const` by value return, a shared queue Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-03-05 08:43 -0800
                Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-03-06 15:51 +0100
              Re: Found a use case for `const` by value return, a shared queue scott@slp53.sl.home (Scott Lurndal) - 2023-02-07 16:35 +0000
                Re: Found a use case for `const` by value return, a shared queue Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-22 06:28 -0800
                Re: Found a use case for `const` by value return, a shared queue scott@slp53.sl.home (Scott Lurndal) - 2023-02-22 15:01 +0000
                Re: Found a use case for `const` by value return, a shared queue Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-02-24 08:34 -0800
          Re: Found a use case for `const` by value return, a shared queue Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-07 01:02 +0200
            Re: Found a use case for `const` by value return, a shared queue scott@slp53.sl.home (Scott Lurndal) - 2023-02-07 01:27 +0000
              Re: Found a use case for `const` by value return, a shared queue Paavo Helde <eesnimi@osa.pri.ee> - 2023-02-07 08:55 +0200
                Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-02-07 12:11 -0800
          Re: Found a use case for `const` by value return, a shared queue James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-02-07 00:19 -0500
            Re: Found a use case for `const` by value return, a shared queue Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-06 21:41 -0800
            Re: Found a use case for `const` by value return, a shared queue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-08 16:49 +0000
              Re: Found a use case for `const` by value return, a shared queue red floyd <no.spam.here@its.invalid> - 2023-02-08 11:11 -0800
                Re: Found a use case for `const` by value return, a shared queue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-09 00:40 +0000
                Re: Found a use case for `const` by value return, a shared queue Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-02-08 17:15 -0800
                Re: Found a use case for `const` by value return, a shared queue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-09 03:07 +0000
                Re: Found a use case for `const` by value return, a shared queue "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-02-09 04:54 +0100
                Re: Found a use case for `const` by value return, a shared queue Öö Tiib <ootiib@hot.ee> - 2023-02-08 23:38 -0800
                Re: Found a use case for `const` by value return, a shared queue David Brown <david.brown@hesbynett.no> - 2023-02-09 09:10 +0100
  Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-03 19:51 +0100
    Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-03-03 12:45 -0800
      Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 05:38 +0100
        Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-03-04 15:25 -0800
          Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-05 05:19 +0100
            Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-03-05 13:41 -0800
              Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-06 16:11 +0100
    Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-04 09:53 +0000
      Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 15:38 +0100
        Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-04 15:28 +0000
          Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 17:17 +0100
            Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-04 16:21 +0000
              Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 17:39 +0100
                Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-04 16:57 +0000
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 18:06 +0100
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 18:10 +0100
                Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-04 17:14 +0000
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 18:41 +0100
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-04 20:07 +0100
                Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-03-04 15:26 -0800
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-05 05:18 +0100
                Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-03-05 13:37 -0800
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-06 05:52 +0100
                Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-03-06 15:29 -0800
                Re: Found a use case for `const` by value return, a shared queue "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-03-06 15:42 -0800
                Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-06 09:18 +0000
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-06 11:18 +0100
                Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-06 10:26 +0000
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-06 16:10 +0100
                Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-06 17:07 +0000
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-07 06:44 +0100
                Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-07 09:31 +0000
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-07 14:45 +0100
                Re: Found a use case for `const` by value return, a shared queue Muttley@dastardlyhq.com - 2023-03-07 15:59 +0000
                Re: Found a use case for `const` by value return, a shared queue Bonita Montero <Bonita.Montero@gmail.com> - 2023-03-07 17:37 +0100

csiph-web