C++ return: brackets considered harmful? [on hold]

(Preliminary note: we had an interesting side discussion here, but obviously the question has deserved an own place.)

Take this innocuous C++17 code:

int main() {     return 0; } 

and compare it with

int main() {     return(0); } 

To the naked eye, both programs appear to do the exactly the same. They even may give the same binary (after optimization). I would expect the runtime behaviour to be the same… but that is not always the case!

Apparently the std::type_info for both functions is different – it’s int() vs int&(). The brackets may even lead to unexpected/different behaviour in code which relies on type information:

int i=0;  decltype(auto) f() { return i; } decltype(auto) g() { return(i); }  if (std::is_same<decltype(f()), decltype(g())>()) {     std::clog << "same" << std::endl; } else {     std::clog << "different" << std::endl; } 

Is writing “return with brackets” not only superfluous, but actually harmful in C++17?

Fetch then apply promise, but return both results

New to RxJS. And redux-observable.

I am having trouble I am trying to dispatch an action midstream of(takeAction()) in code below, then listen to responding actions from this, either a RESOLVE_TAKE type or REJECT_TAKE type. However my of(takeAction) is not triggering, would anyone know how to fix this code below?

As an aside from this question, a bonus: Any style advice on how to restructure my code, I’m not sure if its the cleanest most readable way. I am doing a fetch, then I want to pass the res (response of fetch) and reply (body of fetch, and turn it to json if it is possible) to takeAction.

action$  .pipe(     ofType(START_FOO),     switchMap({ url } =>         from(fetch(url)).pipe(             mergeMap(res => from(res.text()).pipe(                 mergeMap(reply => {                     try { reply = JSON.parse(reply) } catch(ignore) {}                     switch (res.status) {                         case 200: {                             return of(takeAction(res, reply)).pipe( // not dispatching                                 action$  .pipe(                                     ofType(RESOLVE_TAKE, REJECT_TAKE),                                     mergeMap(({ type }) => {                                         if (type === RESOLVE_TAKE) {                                             return of(resolveFooAction())                                         } else {                                             return of(rejectFooAction())                                         }                                     })                                 )                             )                         }                         // other res.status cases go here                     }                 })             )         )     ) ) 

How to prevent lifetime on impl Trait return type

I want to provide an API to generically send some data like this:

fn post<T: ?Sized>(resource: &str, data: &T) -> impl Future<Item = (), Error = ()> where     T: Serialize; 

(Since T: Serialize implies &T: Serialize I’ve tried changing the API to take a value instead of a reference in my examples below without much luck.)

My problem arises when the type T is a struct which itself has references inside. It seems that in that case Rust automatically assumes that these references will be part of the return value. The compiler error message looks like this:

error: cannot infer an appropriate lifetime   --> src/lib.rs:80:28    | 79 | fn post_message<'a>(data: Message<'a>) -> impl Future<Item = (), Error = ()> {    |                                                 ---------------------------------- this return type evaluates to the `'static` lifetime... 80 |     post("messages", data)    |                      ^^^^ ...but this borrow...    | note: ...can't outlive the lifetime 'a as defined on the function body at 79:23   --> src/lib.rs:79:23    | 79 | fn post_message<'a>(data: Message<'a>) -> impl Future<Item = (), Error = ()> {    |                 ^^ help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime 'a as defined on the function body at 79:23    | 79 | fn post_message<'a>(data: Message<'a>) -> impl Future<Item = (), Error = ()> + 'a {    |                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  error: aborting due to previous error 

Unfortunately, the suggested fix (adding the 'a lifetime) isn’t helpful because it prevents me from creating the message as a temporary and passing on the Future to a higher level caller.

Here’s the complete code I’m using to reproduce the problem:

#![allow(unused)]  use futures::Future; // 0.1.26 use reqwest; // 0.9.14 use serde::Serialize; // 1.0.90  // This is an internal API I want to use as a building block. fn post_borrowed<T: ?Sized>(resource: &str, data: &T) -> impl Future<Item = (), Error = ()> where     T: Serialize, {     reqwest::r#async::Client::new()         .post(&format!("http://127.0.0.1/{}", resource))         .json(data)         .send()         .map(|_| ())         .map_err(|_| ()) }  // Alternative internal API if the above doesn't work for some reason. fn post_moved<T>(resource: &str, data: T) -> impl Future<Item = (), Error = ()> where     T: Serialize, {     reqwest::r#async::Client::new()         .post(&format!("http://127.0.0.1/{}", resource))         .json(&data)         .send()         .map(|_| ())         .map_err(|_| ()) }  // Example higher level APIs I'm currently exposing.  // These two are not what I want since they consume the string. fn post_string_borrowed(data: String) -> impl Future<Item = (), Error = ()> {     post_borrowed("strings", &data) } fn post_string_moved(data: String) -> impl Future<Item = (), Error = ()> {     post_moved("strings", data) }  // This is actual the API I'm currently exposing. fn post_str_borrowed(data: &str) -> impl Future<Item = (), Error = ()> {     post_borrowed("strings", data) }  // This does not compile without the undesirable 'a bound on the return type. // I believe if I can solve this the solution would apply to the more complicated // case below as well. fn post_str_moved<'a>(data: &'a str) -> impl Future<Item = (), Error = ()> + 'a {     post_moved("strings", data) }  // Example use of the current API. fn send_str() -> impl Future<Item = (), Error = ()> {     // this comes from somewhere     let s = "a message".to_string();     // The future that is returned here does not capture the temporary.     post_str_borrowed(&s) }  // Higher level API I actually want to expose.  Wrapping the string in a struct // and adding some metadata.  The actual implementation is a bit more complex. // I want to use a struct instead of multiple arguments to make the API a bit // safer, e.g. no switching of message and metadata.  #[derive(Serialize, Debug)] struct Message<'a> {     message: &'a str,     metadata: &'a str, }  // One of these is the API I actually want to expose. // I believe the 'a bound is wrong here but the function doesn't compile without it. fn post_message_borrowed<'a>(data: &'a Message<'a>) -> impl Future<Item = (), Error = ()> +'a {     post_borrowed("messages", data) } fn post_message_moved<'a>(data: Message<'a>) -> impl Future<Item = (), Error = ()> + 'a {     post_moved("messages", data) }  // Example use of the new API.  Does not compile because of the 'a bound above. fn send_message() -> impl Future<Item = (), Error = ()> {     // this comes from somewhere     let s = "a message".to_string();     let m = Message {         message: &s,         metadata: "metadata",     };     post_message_moved(m)     // Also doesn't work: post_message_borrowed(&m) }  

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3a3e113edf8c3136c50c91686cc30e33

Buffer overflow return address with null byte, workaround

Practising BO using Free Float FTP Server 1.0 on Windows 7 SP1, done simple jmp esp examples on Win XP where system DLL are without rebase or ASLR, trying to write one for Win7 where I haven’t got any system DLL with rebase or ASLR disabled, I can still exploit it but it won’t survive reboot, so I’m very much left with opcodes from the FTPServer.exe but the problem is that they all use a low stack addresses (0x0040xxxx) , so they all end with NULL byte (eg, \xce\x11\x40\x00 ) and hence terminate my exploit. My question is what are the options, if any to workaround the NULL byte in address I want to use, more specifically I want to know whenever there is an option to use NULL byte at the end of return address if it is listed as a bad character?

Netcat -z does not return if host not found

I’m using Ubuntu 16.04.6 LTS. I’m trying to detect that a cable is connected to a camera serving an rtsp stream. My script is invoking

nc -zv 10.0.0.3 554 

and testing the return value (or stderr)

If the cable is connected it works as expected, returning zero, but if the cable is not connected, the command waits forever.

I’ve tried adding -w 0, and it doesn’t help, nor does -n, but -z is supposed to return immediately anyway.

if the host is known, it returns quickly as expected

$   nc -zv localhost 554 nc: connect to localhost port 554 (tcp) failed: Connection refused 

Is this expected behavior?

Only return taxonomies that are linked to a category & product

What’s the best way, to return taxonomies, but only taxonomies that are linked to a product, within the category, I have a heirachial taxonomy, I want to get all the top level options, once an option is picked, I want to get all the children, but only the children that are linked to a product.

Is this possible?

Currently I’m just getting the immediate children of the first term picked, I don’t know how to filter out the ones that have no matches.

This is the second request:

$  terms = get_terms( 'tyre', array(     'orderby'       => 'name',     'child_of'      => $  term_id,     'hide_empty'    => $  hide_empty )); 

Is there a way to extend this to something like this:

$  terms = get_terms( 'tyre', array(     'orderby'       => 'name',     'category'      => 'tyres',     'only_linked_to_products' => true,     'child_of'      => $  term_id,     'hide_empty'    => $  hide_empty )); 

Obviously I’ve just invented these two properties, but it would help illustrate what I’m trying to do

Why does my genetic algorithm return negative fitness values? [migrated]

I’m trying to minimize the function and then graph it, but I’m getting negative fitness values and I don’t know how or why. I’m using the GA package which basically handles the algorithm for me as long as I send it a fitness function. (documentation can be found here. https://cran.r-project.org/web/packages/GA/vignettes/GA.html)

# Objective function for I-beam cross section area cross <- function(x1, x2, x3, x4) {   2 * x2 * x4 + x3 * (x1 - 2 * x4) # Not possible to be negative } 

Parameters to the genetic algorithm.

crossGA <- ga(   type = "real-valued",   fitness = function(x)     -cross(x[1], x[2], x[3], x[4]),   lower = c(10, 10, 0.9, 0.9), # Not possible to be negative   upper = c(80, 50, 5, 5), # Not possible to be negative   popSize = 50,   maxiter = 100,   run = 100,   pcrossover = 0.75,   pmutation = .001 ) 

Output.

GA | iter = 1 | Mean = -295.2791 | Best = -104.4886 GA | iter = 2 | Mean = -231.7914 | Best = -104.4886 GA | iter = 3 | Mean = -205.53407 | Best =  -82.49887 GA | iter = 4 | Mean = -199.11156 | Best =  -82.49887 GA | iter = 5 | Mean = -164.27739 | Best =  -82.49887 GA | iter = 6 | Mean = -138.23192 | Best =  -82.49887 GA | iter = 7 | Mean = -117.80066 | Best =  -72.36154 GA | iter = 8 | Mean = -107.05787 | Best =  -72.36154 GA | iter = 9 | Mean = -93.94951 | Best = -72.36154 GA | iter = 10 | Mean = -86.25630 | Best = -72.36154 

Return temperature data in Google Sheets for a specific time of day for each day of the month

I have a column for date and time formatted like so: [3/4/18 1:09] (verified that this is stored as date/time values), and a column for the temperature data at that time in °C like so: [11.536]

There is a reading for every minute and over a month this adds up to over 40K entries.

What I want to do is to make a separate column for the temperature readings for a specific time of day for each day of the month.

So for example I want it to tell me the temperature for 9:00 AM for every day of March.

I’m then going to compare these readings to the daily and monthly temperature averages.

I would like to stick to using functions and not use pivot tables since I’m going to be doing some other things with this later.