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


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

Re: How to make the Process ctor work for different R type?

From wij <wyniijj5@gmail.com>
Newsgroups comp.lang.c++
Subject Re: How to make the Process ctor work for different R type?
Date 2024-11-19 21:16 +0800
Organization A noiseless patient Spider
Message-ID <01b48b009dae2e61e8c73bb09dcd9bdff90ba26a.camel@gmail.com> (permalink)
References <c7d5064e0ec2f7720f1b6cef381666cfbf249cdb.camel@gmail.com> <cone.1732018068.32867.86903.1000@ripper.email-scan.com>

Show all headers | View raw


On Tue, 2024-11-19 at 07:07 -0500, Sam wrote:
> wij writes:
> 
> > struct Process {
> >     template<typename R>
> >     explicit Process(R (*pfunc)(), bool ret_int=false) {
> >       int pid= fork();
> >       if(pid==-1) {
> >         throw "Error";
> >       }
> >       if(pid==0) {
> >         if(ret_int) {
> >           _exit( pfunc() ); // error: invalid use of void expression
> >         } else {
> >           pfunc();         // ret value ignored
> >           _exit(0);
> >         }
> >       }
> >     };
> > }
> > 
> > The issue is _exit(int) require int type.
> > 
> > void f();
> > Process proc(f);   // error: R is void
> > 
> > So, how to make the Process ctor work for different R type?
> 
> For starters this has to be resolved at compile time, not run time.  
> Therefore you can't use ret_int, whose purpose is very obvious, because you  
> don't know its value until runtime. Just because it has a default value  
> doesn't turn it into a compile-time, constexpr value.
> 
> Fortunately there is a very convenient R here. So the first step is to get  
> rid of the useless `ret_int`.
> 
>     template<typename R>
>     explicit Process(R (*pfunc)()) {
> 
> And now this becomes a boring case of constexpr if:
> 
> #include <type_traits>
> 
> // …
> 
> 
>     if constexpr(!std::is_same_v<R,void>)
>       _exit( pfunc() );
>     } else {
>          pfunc();
>          _exit(0);
>     }

Thanks. I need C++11 way.

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


Thread

How to make the Process ctor work for different R type? wij <wyniijj5@gmail.com> - 2024-11-19 10:38 +0800
  Re: How to make the Process ctor work for different R type? red floyd <no.spam.here@its.invalid> - 2024-11-18 20:34 -0800
    Re: How to make the Process ctor work for different R type? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-11-19 01:04 -0500
      Re: How to make the Process ctor work for different R type? Muttley@DastartdlyHQ.org - 2024-11-19 08:27 +0000
        Re: How to make the Process ctor work for different R type? Paavo Helde <eesnimi@osa.pri.ee> - 2024-11-19 10:45 +0200
          Re: How to make the Process ctor work for different R type? Muttley@DastartdlyHQ.org - 2024-11-19 09:15 +0000
      Re: How to make the Process ctor work for different R type? red floyd <no.spam.here@its.invalid> - 2024-11-20 07:23 -0800
        Re: How to make the Process ctor work for different R type? scott@slp53.sl.home (Scott Lurndal) - 2024-11-20 15:47 +0000
  Re: How to make the Process ctor work for different R         type? Sam <sam@email-scan.com> - 2024-11-19 07:07 -0500
    Re: How to make the Process ctor work for different R type? wij <wyniijj5@gmail.com> - 2024-11-19 21:16 +0800
      Re: How to make the Process ctor work for different R type? Paavo Helde <eesnimi@osa.pri.ee> - 2024-11-19 16:15 +0200
      Re: How to make the Process ctor work for different R         type? Sam <sam@email-scan.com> - 2024-11-20 07:51 -0500
  Re: How to make the Process ctor work for different R type? Bonita Montero <Bonita.Montero@gmail.com> - 2024-11-19 16:13 +0100
    Re: How to make the Process ctor work for different R type? wij <wyniijj5@gmail.com> - 2024-11-20 00:30 +0800
      Re: How to make the Process ctor work for different R type? Bonita Montero <Bonita.Montero@gmail.com> - 2024-11-19 17:42 +0100
        Re: How to make the Process ctor work for different R type? Bonita Montero <Bonita.Montero@gmail.com> - 2024-11-21 15:27 +0100
          Re: How to make the Process ctor work for different R type? Muttley@DastartdlyHQ.org - 2024-11-21 16:11 +0000
            Re: How to make the Process ctor work for different R type? Bonita Montero <Bonita.Montero@gmail.com> - 2024-11-21 17:59 +0100
              Re: How to make the Process ctor work for different R type? Muttley@DastartdlyHQ.org - 2024-11-22 09:54 +0000
          Re: How to make the Process ctor work for different R type? wij <wyniijj5@gmail.com> - 2024-11-22 00:32 +0800

csiph-web