Can You Pass by Reference While Using the Ternary Operator

Can you pass by reference while using the ternary operator?

Simple answer: no. You'll have to take the long way around with if/else. It would also be rare and possibly confusing to have a reference one time, and a value the next. I would find this more intuitive, but then again I don't know your code of course:

if(!isset($_SESSION['foo'])) $_SESSION['foo'] = false;
$x = &$_SESSION['foo'];

As to why: no idea, probably it has to with at which point the parser considers something to be an copy of value or creation of a reference, which in this way cannot be determined at the point of parsing.

Why can't we use pass in a ternary statement?

pass is a statement and not an expression.

An expression can be used just about anywhere.

Most statements have their special syntax, usually on a line of their own.

For more information about the difference between the two, see this answer.

Assigning variables by reference and ternary operator?

Here you go

$target = ($result ? &$success : &$errors);

Also your example has two typos



edit

http://php.net/manual/en/language.operators.comparison.php

Note: Please note that the ternary operator is an expression, and that it doesn't evaluate to a variable, but to the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.

idk if this worked before, but it doesn't anymore. if you don't wanna use an if statement, then try this:

$result ? $target = &$success : $target = &$errors;

or on separated lines ...

$result 
? $target = &$success
: $target = &$errors;

Constructor Reference in a ternary operator Java 8

Is there a one-liner solution for the first block?

Yes, you're overthinking it:

return isDangerous ? new Shark() : new Cat();

The operands of the conditional operator are only evaluated when required, so this will only create one instance.



I got this error message: The target type of this expression must be a functional interface

That's because you're not actually returning an Animal, but rather a method reference to a thing which takes no arguments and returns an Animal. So, you could have written (amongst other things):

public Supplier<Animal> createAnimal(boolean isDangerous) {
return isDangerous ? Shark::new : Cat::new;
}

Passing method argument through ternary operator in java

Every expression in Java has a type. There are some complicated rules in the Java Language Specification, in the section on the conditional operator that tell us how to find the type of a conditional expression such as 5 > 8 ? 5 : "ha". But in simple terms, you always get the most specific type that both the second and third arguments are members of.

  • For 5 > 8 ? 5 : 8, both 5 and 8 are int, so this whole expression has type int.
  • For 5 > 8 ? "he" : "ha", both "he" and "ha" are String, so this whole expression has type String.
  • For 5 > 8 ? 5 : "ha", the most specific type that fits both 5 and "ha" is Object. So this whole expression has type Object.

Now since you have versions of test that accept int and accept String, the expressions test ( 5 > 8 ? 5 : 8 ) and test ( 5 > 8 ? "he" : "ha" ) both compile.

But if you don't have a version of test that accepts Object, then test ( 5 > 8 ? 5 : "ha" ) can't compile.

This is an over-simplification. The rules are significantly more complicated than I've described, but this is mostly because they consider the various cases involving null operands, auto-boxing and auto-unboxing.

Using ternary operator to initialize a reference variable?

No, it's just fine. It would not create undefined behavior in this code. You will just change value of a or b to 5, according to condition.

What object is produced by ternary operator in C++?

First you have to undesund what is the type of ternary operator result.

x ? a : std::nullopt;

Here a is variable which can be reference and std::nullopt is something which is implicitly converted to optional of matching type (here std::optional<int>).
So conversion of std::nullopt ends with creation of temporary value. To match type a is also copied.

So ternary operator deduces type to be a value of type std::optional<int> which becomes a temporary object. New instance of std::optional<int> is created.

Now const auto & is able to prolong lifetime of temporaries. So b is reference to std::optional<int> which is a temporary of prolonged lifetime.

d is same scenario, but it is more explicit.

c has (const std::optional<int> &)std::nullopt which is creates temporary object of std::optional<int> with prolonged lifetiem. Here ternary operator has as argument something what is std::optional<int>& and const std::optional<int>& so it is able to pass first arguments reference return type.

See what cppinsights generates with your code:

#include <optional>
#include <iostream>

int main()
{
std::optional<int> a = std::optional<int>();
constexpr const bool x = true;
const std::optional<int> & b = x ? std::optional<int>(a) : std::optional<int>(std::nullopt_t(std::nullopt));
std::cout.operator<<((&a == &b));
const std::optional<int> & c = x ? a : static_cast<const std::optional<int>>(std::optional<int>(std::nullopt_t(std::nullopt)));
std::cout.operator<<((&a == &c));
const std::optional<int> & d = (x ? std::optional<int>(a) : static_cast<const std::optional<int>>(std::optional<int>(std::nullopt_t(std::nullopt))));
std::cout.operator<<((&a == &d));
}


Related Topics



Leave a reply



Submit