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?
Also, use exit(), not _exit(). The former is ISO C++, the latter
is not.
On Tue, 19 Nov 2024 01:04:23 -0500
James Kuyper <[email protected]> boring babbled:
On 11/18/24 23:34, red floyd wrote:
....
Also, use exit(), not _exit(). The former is ISO C++, the latter
is not.
Note, however, that _Exit() has been part of ISO C++'s version of the C
standard library since 2011. It was originally added in C99, and _exit()
was added to POSIX in 2001, and is defined as equivalent to _Exit().
exit() flushes buffers before the process exits (in *nix, don't know about windows), _exit() doesn't.
On 11/18/24 23:34, red floyd wrote:
....
Also, use exit(), not _exit(). The former is ISO C++, the latter
is not.
Note, however, that _Exit() has been part of ISO C++'s version of the C >standard library since 2011. It was originally added in C99, and _exit()
was added to POSIX in 2001, and is defined as equivalent to _Exit().
On 19.11.2024 10:27, [email protected] wrote:
On Tue, 19 Nov 2024 01:04:23 -0500
James Kuyper <[email protected]> boring babbled:
On 11/18/24 23:34, red floyd wrote:
....
Also, use exit(), not _exit(). The former is ISO C++, the latter
is not.
Note, however, that _Exit() has been part of ISO C++'s version of the C
standard library since 2011. It was originally added in C99, and _exit() >>> was added to POSIX in 2001, and is defined as equivalent to _Exit().
exit() flushes buffers before the process exits (in *nix, don't know about >> windows), _exit() doesn't.
Even more importantly, exit() executes the destructors of static objects
and atexit handlers, whereas _exit() does not. So, _exit() is only
suitable for emergency exit in case of errors (failing to join all child >threads, for example).
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.
On 11/18/24 23:34, red floyd wrote:Thank you. I was unaware of that.
...
Also, use exit(), not _exit(). The former is ISO C++, the latter
is not.
Note, however, that _Exit() has been part of ISO C++'s version of the C standard library since 2011. It was originally added in C99, and _exit()
was added to POSIX in 2001, and is defined as equivalent to _Exit().
On 11/18/2024 10:04 PM, James Kuyper wrote:
On 11/18/24 23:34, red floyd wrote:Thank you. I was unaware of that.
...
Also, use exit(), not _exit(). The former is ISO C++, the latter
is not.
Note, however, that _Exit() has been part of ISO C++'s version of the C
standard library since 2011. It was originally added in C99, and _exit()
was added to POSIX in 2001, and is defined as equivalent to _Exit().
But a proper C++-ish wrapper would look like this:^^^
template<typename Fn>
void xfork( Fn &&&fn )
if constexpr( same_as<invoke_result_t<Fn>, int> )
Am 21.11.2024 um 17:11 schrieb [email protected]:
On Thu, 21 Nov 2024 15:27:59 +0100
Bonita Montero <[email protected]> boring babbled:
But a proper C++-ish wrapper would look like this:^^^
template<typename Fn>
void xfork( Fn &&&fn )
What?
Just a typo.
if constexpr( same_as<invoke_result_t<Fn>, int> )
What?
Have you got something against simply passing std::function ?
A std::function isn't necessary here since I don't need any type erasure
with that.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 05:45:25 |
| Calls: | 12,100 |
| Calls today: | 8 |
| Files: | 15,003 |
| Messages: | 6,517,914 |
| Posted today: | 1 |