标签: conditional-operator

IE条件运算符:OR ...如果大于ie9或不是IE

我想只包含历史记录和ajaxify如果浏览器是ie9或更高,或者不是 ie:

<!--[if gte IE 9]>
    <script type="text/javascript" src="assets/js/plugins/history.js"></script>
    <script type="text/javascript" src="assets/js/plugins/ajaxify.js"></script>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

我怎样才能使用OR运算符来说出这个:

<!--[if gte IE 9 | !IE ]> ??

谢谢!

html internet-explorer conditional-operator conditional-statements

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

比较PHP中的多个值

我想离开这个:

if($var == 3 || $var == 4 || $var == 5 || $var =='string' || $var == '2010-05-16') {
   // execute code here
}
Run Code Online (Sandbox Code Playgroud)

对此:

if($var == (3, 4, 5, 'string', '2010-05-16')) { // execute code here }
Run Code Online (Sandbox Code Playgroud)

看起来非常冗余以便继续输入$var,我发现它使得阅读有点麻烦.PHP中有没有办法以这种方式简化它?我在这里阅读一篇文章,在使用XQuery时你可以使用=运算符$var = (1,2,3,4,5)等.

php conditional-operator

13
推荐指数
4
解决办法
3万
查看次数

条件vs运算符?

在GCC(版本4.8.2)手册中,陈述如下:

-ftree-loop-if-convert-stores:
尝试if-convert包含内存写入的条件跳转.这种转换对于多线程程序来说可能是不安全的,因为它将条件内存写入转换为无条件内存写入.例如,

   for (i = 0; i < N; i++)
      if (cond)
        A[i] = expr;
Run Code Online (Sandbox Code Playgroud)

变成了

   for (i = 0; i < N; i++)
       A[i] = cond ? expr : A[i];
Run Code Online (Sandbox Code Playgroud)

可能产生数据竞争.

但是,我想知道使用operator?if语句相比是否有性能提升.

  • 在第一段代码中,在满足条件时才A[i]设置为.如果不满足,则跳过语句中的代码.expr
  • 在第二个,A[i]似乎写不管条件; 条件只影响它设置的值.

通过使用operator?,我们也在做检查; 但是,在不满足条件的情况下,我们会增加一些开销.我错过了什么吗?

c c++ gcc if-statement conditional-operator

13
推荐指数
1
解决办法
409
查看次数

具有条件(三元)表达式的运算符'sizeof'

sizeof在给出三元表达时,我很难理解行为.

#define STRING "a string"

int main(int argc, char** argv)
{
  int a = sizeof(argc > 1 ? STRING : "");

  int b = sizeof(STRING);
  int c = sizeof("");

  printf("%d\n" "%d\n" "%d\n", a, b, c);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在此示例中(使用gcc 4.4.3和4.7.2进行测试,编译时-std=c99),b为9(8个字符+隐式'\0'),c为1(隐式'\0').a,出于某种原因,是4.

根据argc是否大于1,我希望a为9或1.我想也许字符串文字在被传递到之前转换为指针sizeof,导致sizeof(char*)为4.

我尝试替换STRING""char数组...

char x[] = "";
char y[] = "a string";
int a = sizeof(argc > 1 ? x : y);
Run Code Online (Sandbox Code Playgroud)

...但我得到了相同的结果(a = …

c sizeof conditional-operator

13
推荐指数
2
解决办法
1103
查看次数

为什么这个函数在给定rvalue参数的情况下返回左值引用?

以下定义了一个min函数

template <typename T, typename U>
constexpr auto
min(T&& t, U&& u) -> decltype(t < u ? t : u)
{
    return t < u ? t : u;
}
Run Code Online (Sandbox Code Playgroud)

有一个问题:它似乎写得完全合法

min(10, 20) = 0;
Run Code Online (Sandbox Code Playgroud)

这已经过Clang 3.5和g ++ 4.9的测试.

解决方案很简单,只是std::forward用来恢复参数的"rvalue-ness",即修改正文和decltype

t < u ? std::forward<T>(t) : std::forward<U>(u)
Run Code Online (Sandbox Code Playgroud)

但是,我无法解释为什么第一个定义不会产生错误.


鉴于我的转发和普遍引用,两者的理解tu演绎他们的论据类型,int&&当传递整数常量.但是,在正文中min,参数有名称,因此它们是左值.现在,条件运算符真正复杂规则发挥作用,但我认为相关的是:

  • E2 [和] E3都是相同类型的glvalues.在这种情况下,结果具有相同的类型和值类别.

因此,返回类型operator?:应该是int&&为好,应该不是吗?但是,(据我所知),Clang和g ++都 …

c++ conditional-operator rvalue-reference c++11 universal-reference

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

什么'返回x?:1`表示C语言?

我在Linux内核源代码(2.6.32)中遇到了以下代码.

do_wait_for_common(struct completion *x, long timeout, int state)
{
        if (!x->done) {
        /* some code here */
        }
        x->done--;
        return timeout ?: 1; <--- What it returns?
}
Run Code Online (Sandbox Code Playgroud)

为了理解这种行为,我手动尝试了以下代码

#include <stdio.h>
int f(int x)
{
        return x?:1;
}
int main()
{
        printf("f %d\n", f(0));
        printf("f %d\n", f(1));
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

并得到以下输出

f 1
f 1
Run Code Online (Sandbox Code Playgroud)

当我改变它

int f(int x)
{
        return x?:2;
}
Run Code Online (Sandbox Code Playgroud)

我正进入(状态

f 2
f 1
Run Code Online (Sandbox Code Playgroud)

我只是想知道标准中是否提到了这种行为(如果没有提到则返回1).

c ternary-operator conditional-operator

13
推荐指数
3
解决办法
1191
查看次数

C# 中的双问号等号 (??=) 是什么?

我经常看到这样的言论:

int? a = 5;

//...other code

a ??= 10;
Run Code Online (Sandbox Code Playgroud)

??=第二行是什么意思?我以前见过??用于空合并,但我从未见过它与等号一起使用。

c# conditional-operator equals-operator

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

cout语句中使用的条件运算符

通过尝试,我开始知道有必要在cout语句中将括号放在条件运算符周围.这是一个小例子:

#include <iostream>

int main() {
  int a = 5;
  float b = (a!=0) ? 42.0f : -42.0f;
  // works fine
  std::cout << b << std::endl;
  // works also fine
  std::cout << ( (a != 0) ? 42.0f : -42.0f ) << std::endl;
  // does not work fine
  std::cout << (a != 0) ? 42.0f : -42.0f;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

42
42
1
Run Code Online (Sandbox Code Playgroud)

为什么这些括号必要?在两种情况下都知道条件运算符的结果类型,不是吗?

c++ conditional-operator

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

有条件的?:具有类构造函数的运算符

有人可以解释我为什么,cc1以不同的方式构建.我明白我参考了'?'创建的副本 操作员,在施工后被摧毁,但为什么在第一种情况下它表现出其他方式.我已经测试过它的优化,但即使从控制台读取条件,我也有相同的结果.提前致谢

#include <vector>

class foo {
public:
    foo(const std::vector<int>& var) :var{ var } {};
    const std::vector<int> & var;
};

std::vector<int> f(){
    std::vector<int> x{ 1,2,3,4,5 };
    return x;
};

int main(){
    std::vector<int> x1{ 1,2,3,4,5 ,7 };
    std::vector<int> x2{ 1,2,3,4,5 ,6 };
    foo c{ true ? x2 : x1 };    //c.var has expected values 
    foo c1{ true ? x2 : f() };  //c.var empty 
    foo c2{ false ? x2 : f() };  //c.var empty 
    foo c3{ x2 …
Run Code Online (Sandbox Code Playgroud)

c++ conditional-operator c++14

12
推荐指数
1
解决办法
532
查看次数

为什么C ++不允许条件运算符中的隐式列表初始化?

此代码编译:

std::string f(bool a, std::string const& b)
{
    if (a) return b;
    return {};
}
Run Code Online (Sandbox Code Playgroud)

该代码还可以编译:

std::string f(bool a, std::string const& b)
{
    return a ? b : std::string{};
}
Run Code Online (Sandbox Code Playgroud)

此代码无法编译:

std::string f(bool a, std::string const& b)
{
    return a ? b : {};
}
Run Code Online (Sandbox Code Playgroud)

假设?:运算符的两个结果值必须是同一类型,为什么它不像第一个示例中那样推断类型?


看来,这个问题可能有类似的答案,(基本上可以归结为“因为没有人想过写的语言规范时”)。但是,我仍然认为保留该问题很有用,因为问题本身是不同的,这仍然足够令人惊讶,并且其他人不会在搜索此问题时出现。

c++ conditional-operator language-lawyer c++11 list-initialization

12
推荐指数
1
解决办法
351
查看次数