我正在使用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)
父级正确设置为NULL或p依赖.这就是我所期待的.但是,如果敢于懒惰并写下:
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
有没有办法在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)
广告似乎非常不优雅.
我想在.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.
这段代码不起作用.
(注意:以下情况仅仅是示例性的.这个问题适用于任何可以评估为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
我有什么理由不能使用以下代码吗?
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)
我知道如何解决这个问题.我只是想知道原因.
谢谢.
为此使用三元运算符是否是一个好习惯:
answersCounter = answer.length != 0 ? ++answersCounter : answersCounter;Run Code Online (Sandbox Code Playgroud)
这是我经常问自己的一个问题,因为它经常发生。或者,使用普通的 if 语句更好吗?对我来说,这看起来更简洁。
给定以下两个功能:
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)
产生以下程序集:
Run Code Online (Sandbox Code Playgroud)main: and edi, 1 mov eax, OFFSET FLAT:f() mov …
c++ optimization function-pointers conditional-operator compiler-optimization
在函数“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) 使用时std::weak_ptr,最佳实践是使用std::shared_ptr该lock()方法访问相应的内容,如下所示:
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)
将是有效的代码,但这不能编译。
是否可以将这种方法与三元运算符一起使用?
假设我们有一些不可复制和不可移动的类型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)
这不会编译,因为foo和Foo{ 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++ ×5
python ×2
c# ×1
coding-style ×1
conditional ×1
destructor ×1
dictionary ×1
el ×1
expression ×1
javascript ×1
jsf ×1
optimization ×1
prvalue ×1
return ×1
ulong ×1