c++ - What's the purpose of the void cast here? -


this question has answer here:

i have been looking @ journalctl code , came across following block of code.

it seems shorthand way of exiting out of series of condition tests if there non-zero result conditions being tested. pretty clever.

but i'm unsure purpose of void cast. suppress compiler output? g++ not care either way -wall , -pedantic -pedantic-errors.

m1 = strjoina("_systemd_unit=", unit); m2 = strjoina("coredump_unit=", unit); m3 = strjoina("unit=", unit); m4 = strjoina("object_systemd_unit=", unit);  (void)(   /* messages service */   (r = sd_journal_add_match(j, m1, 0)) ||    /* coredumps of service */   (r = sd_journal_add_disjunction(j)) ||   (r = sd_journal_add_match(j, "message_id=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||   (r = sd_journal_add_match(j, "_uid=0", 0)) ||   (r = sd_journal_add_match(j, m2, 0)) ||    /* messages pid 1 service */   (r = sd_journal_add_disjunction(j)) ||   (r = sd_journal_add_match(j, "_pid=1", 0)) ||   (r = sd_journal_add_match(j, m3, 0)) ||    /* messages authorized daemons service */   (r = sd_journal_add_disjunction(j)) ||   (r = sd_journal_add_match(j, "_uid=0", 0)) ||   (r = sd_journal_add_match(j, m4, 0)) );  f (r == 0 && endswith(unit, ".slice")) {    ... 

  1. to suppress compiler warnings unused result of logical expression. logiocal-or expression built side-effects , short-circuit evaluation properties. author of code not care final result though. compiler might not smart enough realize that. might warn final result being discarded.
  2. to convey human readers intent of code's author discard result of logical expression.

explicit conversion void accepted idiom conveys intent (to both compiler , human readers).

p.s. application of || 1 example of branching in "classic" c-style expression programming, described here: https://stackoverflow.com/a/1618867/187690


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -