有没有办法输入如果喜欢:
var = (cond) ? true : false;
Run Code Online (Sandbox Code Playgroud)
还是我们必须使用这种格式?
if (cond)
true
else
false
end
Run Code Online (Sandbox Code Playgroud) 条件(三元)运算符表明三元运算符是if...的替代品else。我一直这么认为,但最近我有一个逻辑问题。
考虑这个简短的调试会话:
DB<1> $s='X'
DB<2> 1 ? $s .= '_' : $s = '_'
DB<3> x $s
0 '_'
Run Code Online (Sandbox Code Playgroud)
因此,如果为 true,则应该计算1表达式(而不是)。$s .= '_'$s = '_'
但为什么$s只是'_'最后呢?
我无法找到规范的相关部分来回答这个问题.在Java中的条件运算符语句中,是否评估了true和false参数?
因此,以下内容可能会抛出NullPointerException
Integer test = null;
test != null ? test.intValue() : 0;
Run Code Online (Sandbox Code Playgroud) 在VBA中,我可以执行以下操作:
A = B + IIF(C>0, C, 0)
Run Code Online (Sandbox Code Playgroud)
所以,如果C> 0,我得到A=B+C,C <= 0,我得到A=B
是否有一个运算符或函数可以让我在MATLAB代码中内联这些条件?
我正在查看OpenSSL中使用的一些预处理器宏,我发现了以下内容crypto/stack/safestack.h:
#define CHECKED_STACK_OF(type, p) \
((_STACK*) (1 ? p : (STACK_OF(type)*)0))
#define CHECKED_SK_FREE_FUNC(type, p) \
((void (*)(void *)) ((1 ? p : (void (*)(type *))0)))
#define CHECKED_SK_FREE_FUNC2(type, p) \
((void (*)(void *)) ((1 ? p : (void (*)(type))0)))
Run Code Online (Sandbox Code Playgroud)
我猜它的编写方式可以解决编译器错误(可能是供应商十多年来一直没有支持的古老版本).
使用1上述内容的目的是什么,因为它始终如一?
我正在查看一些带有巨大的switch语句和每个case的if-else语句的代码,并立即感受到优化的冲动.作为一个优秀的开发人员,我总是应该着手获得一些硬性时序事实,并从三个变体开始:
原始代码如下所示:
public static bool SwitchIfElse(Key inKey, out char key, bool shift)
{
switch (inKey)
{
case Key.A: if (shift) { key = 'A'; } else { key = 'a'; } return true;
case Key.B: if (shift) { key = 'B'; } else { key = 'b'; } return true;
case Key.C: if (shift) { key = 'C'; } else { key = 'c'; } return true;
...
case Key.Y: if (shift) { key = 'Y'; } else { key …Run Code Online (Sandbox Code Playgroud)c# performance if-statement conditional-operator micro-optimization
条件运算符适用于许多属性,如"已渲染","值"等.
但它不起作用?或者我做错了吗?
<h:commandLink action="#{true ? bean.methodTrue() : bean.methodFalse()}"/>
Run Code Online (Sandbox Code Playgroud)
错误:javax.el.ELException:不是有效的方法表达式
(我用primefaces ajax action属性实现了它)
我习惯于编写带有文件名或读取的小命令行工具std::cin,所以我一直在使用这种模式:
int main(int argc, char* argv[])
{
std::string filename;
// args processing ...
std::ifstream ifs;
if(!filename.empty())
ifs.open(filename);
std::istream& is = ifs.is_open() ? ifs : std::cin;
std::string line;
while(std::getline(is, line))
{
// process line...
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在阅读Stack Overflow上的一个问题之后,我尝试修改我常用的模式,以满足从文件或文件中读取的需要std::istringstream.令我惊讶的是它不会编译并给出这个错误:
Run Code Online (Sandbox Code Playgroud)temp.cpp:164:47: error: invalid initialization of non-const reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘void*’ std::istream& is = ifs.is_open() ? ifs : iss; // won't compile
在我看来,它试图将std::istringstreamobject(iss)转换为布尔值并获取它operator void*(). …
这段代码工作正常:-
Integer nullInt = null;
if (1 <= 3) {
Integer secondNull = nullInt;
} else {
Integer secondNull = -1;
}
System.out.println("done");
Run Code Online (Sandbox Code Playgroud)
但这会引发空指针异常,而 Eclipse 警告需要自动拆箱:-
Integer nullInt = null;
Integer secondNull = 1 <= 3 ? nullInt : -1;
System.out.println("done");
Run Code Online (Sandbox Code Playgroud)
为什么会这样,有人可以指导吗?
众所周知,throw可以作为C++三元运算符的第二个或第三个操作数放置?:。但是它可以在操作数的逗号子表达式中吗?看起来编译器在这方面存在分歧。请考虑一个例子:
#include <iostream>
void foo(bool b) {
int i = b ? 1 : (throw 0); //ok everywhere
if ( !b )
(std::cout << "smth\n", throw 0); //ok everywhere
i = b ? 2 : (std::cout << "smth\n", throw 0); //ok in MSVC only
};
Run Code Online (Sandbox Code Playgroud)
这个例子被 MSVC 接受,但被 GCC 和 Clang 拒绝,演示:https : //gcc.godbolt.org/z/6q46j5exP
虽然错误信息:
#include <iostream>
void foo(bool b) {
int i = b ? 1 : (throw 0); //ok everywhere
if ( !b …Run Code Online (Sandbox Code Playgroud)