标签: conditional-operator

条件运算符的局限性?:

我正在使用GCC 4.5,并观察到非常奇特的行为.我想知道这个运算符是否有什么我不完全理解的东西.我以为我精通C++.我有一个Wnd用于Windows HWND对象的精简C++包装类,它具有一个实现的强制转换操作符operator HWND ....

如果我像这样使用条件运算符(给定输入Wnd *p和示例函数SetParent(HWND)):

SetParent((p!=NULL) ? (HWND)(*p) : NULL)
Run Code Online (Sandbox Code Playgroud)

父级正确设置为NULLp依赖.这就是我所期待的.但是,如果敢于懒惰并写下:

SetParent(p ? *p : NULL)
Run Code Online (Sandbox Code Playgroud)

事情变得混乱.在运行GDB之后,我发现p在调用之后调用了析构函数SetParent.有什么想法在这里发生了什么?

编辑 这是我的Wnd课程:

class Wnd{
        HWND m_hwnd;        ///< the actual handle
        WndFake *fake;      ///< store state here if we do not have a handle
    public:
        virtual ~Wnd();
        //contructor s
        Wnd(HWND wnd=NULL):m_hwnd(wnd),fake(NULL){}
        Wnd(DWORD sty,const jchar *title,const RECT &sz);
        operator HWND(){return m_hwnd;}
        operator HWND() const {return m_hwnd;} …
Run Code Online (Sandbox Code Playgroud)

c++ conditional destructor conditional-operator operator-keyword

6
推荐指数
1
解决办法
796
查看次数

字典上的三元操作

有没有办法在dict上执行三元运算,其中测试是has_key()而没有遇到键错误?即

variable = dict["the_key"] if dict.has_key("the_key") else "something"
Run Code Online (Sandbox Code Playgroud)

(这明显击中了关键错误)

如果没有这个,那么根据一个可能有也可能没有问题的字典来分配一堆变量值的最pythonic方法是什么?重复:

if dict.has_key('key'):
    value = dict['key']
else:
    value = "value"
Run Code Online (Sandbox Code Playgroud)

广告似乎非常不优雅.

python dictionary conditional-operator

6
推荐指数
2
解决办法
3902
查看次数

在h:inputText值和h:commandButton actionListener中使用条件运算符

我想在.xhtml文件中加载两个差异面板.

<h:inputText value="#{param['from']=='TERMINAL' ? terminalsList.globalFilter : merchantsList.globalFilter}" size="50" />
<h:commandButton value="Filter" actionListener="#{param['from']=='TERMINAL' ? terminalsList.filterTerminals : merchantsList.filterMerchants}" />
<h:commandButton value="Reset" actionListener="#{param['from']=='TERMINAL' ? terminalsList.resetTerminalsFilter : merchantsList.resetMerchantsFilter}" />
Run Code Online (Sandbox Code Playgroud)

当http get request params equals'TERMINAL'我想加载'terminalsList'托管bean,否则'merchantsList'托管bean.

这段代码不起作用.

jsf el conditional-operator

6
推荐指数
1
解决办法
2万
查看次数

是'或'在赋值pythonic的右侧使用?

情况

(注意:以下情况仅仅是示例性的.这个问题适用于任何可以评估为bool的事情)

如果用户未提供自定义列表,则应使用默认列表:

default_list = ...
custom_list = ...
if custom_list:
    list = custom_list
else:
    list = default_list
Run Code Online (Sandbox Code Playgroud)

你可以缩短到:

default_list = ...
custom_list = ...
list = custom_list if custom_list else default_list
Run Code Online (Sandbox Code Playgroud)

现在,根据https://docs.python.org/2.7/reference/expressions.html#or ...

表达式x or y首先评估x; 如果x,则返回其值; 否则,将评估y并返回结果值.

...,or不返回布尔值,而是返回布尔转换不为false的第一个值.因此,以下是有效的代码:

list = custom_list or default_list
Run Code Online (Sandbox Code Playgroud)

这与C#Null Coalescing运算符类似,不同之处在于它应该作为False Coalescing Operator在Python中重新生成,它返回第一个非false参数.

最后一个例子似乎更容易阅读,但它被认为是pythonic?

无论PEP8(程序),也不pylint的不抱怨.

python boolean-logic coding-style conditional-operator language-lawyer

6
推荐指数
2
解决办法
290
查看次数

在inline-if中返回ulong

我有什么理由不能使用以下代码吗?

ulong test(int a, int b)
{
    return a == b ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)

它告诉我:

Cannot implicitly convert type 'int' to 'ulong'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)

以下将有效:

ulong test(int a, int b)
{
    return false ? 0 : 1;
}
Run Code Online (Sandbox Code Playgroud)

我知道如何解决这个问题.我只是想知道原因.

谢谢.

c# ternary-operator conditional-operator ulong

6
推荐指数
1
解决办法
153
查看次数

三元运算符用于增加变量

为此使用三元运算符是否是一个好习惯:

answersCounter = answer.length != 0 ? ++answersCounter : answersCounter;
Run Code Online (Sandbox Code Playgroud)

这是我经常问自己的一个问题,因为它经常发生。或者,使用普通的 if 语句更好吗?对我来说,这看起来更简洁。

javascript conditional-operator

6
推荐指数
1
解决办法
2009
查看次数

没有通过gcc或clang优化函数指针的间接访问-错误或预期的?

给定以下两个功能:

int f() { return 0; }
int g() { return 1; }
Run Code Online (Sandbox Code Playgroud)

以下代码根据布尔值调用其中之一b

int t0(bool b) { return (b ? &f : &g)(); }
int t1(bool b) { return b ? f() : g(); }
int t2(bool b) { return b ? t0(true) : t0(false); }
Run Code Online (Sandbox Code Playgroud)

两者g++ (trunk)clang++ (trunk)-std=c++2a -Ofast -march=native失败来优化下面的代码:

int main(int ac, char**) { return t0(ac & 1); }
Run Code Online (Sandbox Code Playgroud)

产生以下程序集:

main:
  and edi, 1
  mov eax, OFFSET FLAT:f()
  mov …
Run Code Online (Sandbox Code Playgroud)

c++ optimization function-pointers conditional-operator compiler-optimization

6
推荐指数
0
解决办法
154
查看次数

'return' 之前的预期主表达式

在函数“int v(std::string)”中:7:17:错误:“return”之前的预期主表达式 7:17:错误:“return”之前预期的“:” 7:17:错误:预期主表达式'return' 8:1 之前的表达式:警告:函数中没有 return 语句返回非 void [-Wreturn-type]

#include<iostream>
#include<string>

using namespace std;

int v(string s) 
{
    s.length()? return 1:return 0;
}

int main()
{
    string s="";
    cout<<v(s);
}
Run Code Online (Sandbox Code Playgroud)

c++ expression compiler-errors return conditional-operator

6
推荐指数
1
解决办法
3886
查看次数

在三元运算符中赋值

使用时std::weak_ptr,最佳实践是使用std::shared_ptrlock()方法访问相应的内容,如下所示:

std::weak_ptr<std::string> w;
std::shared_ptr<std::string> s = std::make_shared<std::string>("test");

w = s;

if (auto p = w.lock())
   std::cout << *p << "\n";
else
   std::cout << "Empty";
Run Code Online (Sandbox Code Playgroud)

如果我想使用三元运算符来简写它,则似乎是这样的:

std::cout << (auto p = w.lock()) ? *p : "Empty";
Run Code Online (Sandbox Code Playgroud)

将是有效的代码,但这不能编译。

是否可以将这种方法与三元运算符一起使用?

c++ conditional-operator

6
推荐指数
1
解决办法
899
查看次数

是否可以将纯右值显式转换为 const 引用以避免三元运算符中的额外复制?

假设我们有一些不可复制和不可移动的类型Foo,以及一个函数

int f(const Foo& foo) {
  ... // somehow compute the result depending on the value of foo.
}
Run Code Online (Sandbox Code Playgroud)

现在假设我们想像这样调用这个函数:

const Foo foo1{ 42 };
bool condition = getCondition();
int result = f(condition ? foo : Foo{ 150 });
Run Code Online (Sandbox Code Playgroud)

这不会编译,因为fooFoo{ 150 }属于不同的值类别,因此,根据定义,三元运算符将返回类型的纯右值Foo- 这意味着foo1需要复制 if condition == true,因此这会导致编译错误。

然而,C++ 保证临时对象在表达式结束之前保持有效,因此我们可以这样写:

const Foo foo1{ 42 };
bool condition = getCondition();
int result = f(condition ? foo1 : static_cast<const Foo&>(Foo{ 150 }); …
Run Code Online (Sandbox Code Playgroud)

c++ conditional-operator prvalue

6
推荐指数
0
解决办法
152
查看次数