标签: unary-operator

在调用std :: numeric_limits <unsigned char>成员之前,一元"+"的目的是什么?

在cppreference的文档中看到了这个例子std::numeric_limits

#include <limits>
#include <iostream>

int main() 
{
    std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";

    std::cout << "uchar\t"
              << +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
              << +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
              << +std::numeric_limits<unsigned char>::max() << '\n';
    std::cout << "int\t"
              << std::numeric_limits<int>::lowest() << '\t'
              << std::numeric_limits<int>::min() << '\t'
              << std::numeric_limits<int>::max() << '\n';
    std::cout << "float\t"
              << std::numeric_limits<float>::lowest() << '\t'
              << std::numeric_limits<float>::min() << '\t'
              << std::numeric_limits<float>::max() << '\n';
    std::cout << "double\t"
              << std::numeric_limits<double>::lowest() << '\t'
              << std::numeric_limits<double>::min() << '\t'
              << std::numeric_limits<double>::max() …
Run Code Online (Sandbox Code Playgroud)

c++ char unary-operator

129
推荐指数
4
解决办法
4070
查看次数

++ someVariable Vs. Javascript中的someVariable ++

在Javascript中,您可以++在变量名称之前或之后使用运算符.这些增量变量的方法之间的区别是什么?

javascript increment unary-operator

122
推荐指数
5
解决办法
5万
查看次数

一元加运算符有什么作用?

一元加运算符有什么作用?我找到了几个定义(这里这里),但我仍然不知道它将用于什么.它似乎没有做任何事情,但有理由,对吗?

c c# c++ unary-operator

75
推荐指数
7
解决办法
4万
查看次数

什么是"int*p = + s;" 做?

我在这里看到了一种奇怪的程序.

int main()
{
    int s[]={3,6,9,12,18};
    int* p=+s;
}
Run Code Online (Sandbox Code Playgroud)

上面的程序在GCCClang编译器上进行了测试,并在两个编译器上都运行良好.

我很想知道,int* p=+s;做了什么?

数组是否已s衰减为指针类型?

c++ arrays pointers unary-operator

63
推荐指数
4
解决办法
7126
查看次数

在这种情况下,C是否正确处理sizeof(...)和sizeof ...

在下面的代码中,功能testtest2等效的是什么?

typedef int rofl;

void test(void) {
    rofl * rofl = malloc(sizeof(rofl)); // Is the final rofl here the TYPE?
}

void test2(void) {
    rofl * rofl = malloc(sizeof *rofl); // Is the final rofl here the VARIABLE?
}
Run Code Online (Sandbox Code Playgroud)

换一种说法:

  1. 是否roflsizeof(rofl)正确挑选rofl 类型,因为括号?
  2. 是否roflsizeof *rofl正确地挑选rofl 可变的,因为一个缺少括号?

注意:这是一个看起来很愚蠢的例子,但在实践中实际上你可以使用与变量名相同的类型名称.因此问题.

c sizeof unary-operator

38
推荐指数
2
解决办法
1748
查看次数

Java的一元加运算符的目的是什么?

Java的一元加运算符似乎来自C,通过C++.

int result = +1;   
Run Code Online (Sandbox Code Playgroud)

它似乎有以下影响:

  • 如果它是包装器对象,则将其操作数取消装箱
  • int如果它不是已经int或更宽,则提升其操作数
  • 稍微复杂化包含大量连续加号的邪恶表达式的解析

在我看来,有更好/更清晰的方法来做所有这些事情.

这个SO问题中,关于C#中的对应运算符,有人说"如果你觉得有必要,它就会超载".

但是,在Java中,不能超载任何运算符.那么这个一元加运算符是否只存在于Java中,因为它存在于C++中?

java unary-operator language-history

36
推荐指数
3
解决办法
7020
查看次数

C:使用无符号操作数的一元减运算符行为

我似乎无法在C标准中找到完全定义带有无符号操作数的一元减运算符的行为的相关部分.

2003 C++标准(是的,C++,我用几行代表)在5.3.1c7中说: The negative of an unsigned quantity is computed by subtracting its value from 2^n, where n is the number of bits in the promoted operand.

然而,1999 C标准并未包含这样一个明确的陈述,也没有明确界定一元行为 - 在6.5.3.3c1,3和6.5c4中都没有.在后者中它说Some operators (the unary operator ~, and the binary operators <<, >>, &, ^, and |, ...) ... return values that depend on the internal representations of integers, and have implementation-defined and undefined aspects for signed types.),它排除了一元减去,事情似乎仍然含糊不清.

这个早期的问题是指K&R ANSI C一书中的A.7.4.5节The negative of an unsigned quantity …

c unary-operator

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

一元+算子有实际用途吗?

一元运算+符是否仅包含与一元运算-符对称,还是在C++代码中找到了一些实际用途?

在这里搜索,我遇到了C中一元'+'运算符的目的是什么?,但唯一有用的场景涉及预处理器宏.这些都是很好的,但它们似乎是一些不常见的情况,并涉及宏.是否有涉及更常见的C++代码的用例?

c++ operators unary-operator

28
推荐指数
4
解决办法
2217
查看次数

不同的间距如何影响一元算子?

谁能解释我不同的间距如何影响一元算子?

int i = 1;
int j = i+ + +i; // this will print j=2
int k = i++ +i; // this will print k=3
int l = i+++i; // this will print l=3
int m = i++++i; // compile time error
Run Code Online (Sandbox Code Playgroud)

.

java unary-operator

27
推荐指数
2
解决办法
990
查看次数

一元运算符"++"和" - "奇怪的情况

这是使用一元运算符"++"的测试情况:

var j = 0 ;
console.log(j);
j = j++;
console.log(j);
Run Code Online (Sandbox Code Playgroud)

为此,输出为:

0
0
Run Code Online (Sandbox Code Playgroud)

由于++运算符的位置位于操作数的后面,因此它的优先级低于赋值的优先级,我希望"j"首先接收自身的值(ie0),然后递增.那么为什么第二个console.log(j)电话仍然显示为"0"?

为了清楚起见,我知道解决方案是:

// 1)
j++;
// 2)
++j;
// 3)
j += 1;
// 4)
j = ++j;
Run Code Online (Sandbox Code Playgroud)

但我需要知道为什么在这个特定情况下不进行增量步骤,而不是如何解决它!

javascript unary-operator

25
推荐指数
2
解决办法
2104
查看次数