PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, December 13, 2022

[FIXED] What is calling void(); doing?

 December 13, 2022     c++, syntax, void     No comments   

Issue

I came across void(); being used as a 'do-nothing' in the 'else' branch of a ternary operator, as a shorthand for a null pointer check

if(var){
   var->member();
}

as

var ? var->member() : void();

but I can't seem to find any reference to the void keyword being used in this way, is this a function or functor call on the void keyword itself? or is it casting nothing to the type of void? or is this just the c++ syntax of something like pass?

Edit: The return type of member() is void in this situation.


Solution

You are just "constructing" a prvalue (not a variable, for the reason suggested in the comments) of type void, just as int() would default-construct an int.

As others said in the comments, the second alternative is pejorative. The ternary operator is, well, ternary because it has the if, the then, and the else parts. If you don't need an else, why would you write one and leave it empty?

That alternative is even uglier and more cryptic than this,

if(var){
   var->member();
} else {}

which may just look stupid.



Answered By - Enlico
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing