为什么最负的int值会导致有关模糊函数重载的错误? [英] Why does the most negative int value cause an error about ambiguous function overloads?

查看:343
本文介绍了为什么最负的int值会导致有关模糊函数重载的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++中的函数重载并遇到过这样的问题:

I'm learning about function overloading in C++ and came across this:

void display(int a)
{
    cout << "int" << endl;
}

void display(unsigned a)
{
    cout << "unsigned" << endl;
}

int main()
{
    int i = -2147483648;
    cout << i << endl; //will display -2147483648
    display(-2147483648);
}

根据我的理解,中给出的任何值int range(在我的情况下 int 是4个字节)将调用 display(int)超出此范围的任何值都将是不明确的(因为编译器无法决定调用哪个函数)。它适用于 int 值的完整范围,除了它的最小值,即 -2147483648 ,其中编译失败,错误

From what I understood, any value given in the int range (in my case int is 4 byte) will call display(int) and any value outside this range will be ambiguous (since the compiler cannot decide which function to call). It is valid for the complete range of int values except its min value i.e. -2147483648 where compilation fails with the error


调用重载显示(long int)是不明确的

但是将相同的值带到 int 并打印该值会给出 2147483648 。我确实对这种行为感到困惑。

But taking the same value to an int and printing the value gives 2147483648. I'm literally confused with this behavior.

为什么只有在传递最多负数时才会观察到这种行为? (如果 -32768 一起使用,行为是相同的 - 实际上,在任何情况下,负数数字和正数具有相同的二进制表示)

Why is this behavior observed only when the most negative number is passed? (The behavior is the same if a short is used with -32768 - in fact, in any case where the negative number and positive number have the same binary representation)

使用的编译器:g ++(GCC)4.8.5

Compiler used: g++ (GCC) 4.8.5

推荐答案

这是一个非常微妙的错误。你所看到的是C ++中没有负整数文字的结果。如果我们看[lex.icon],我们得到整数 - 字面

This is a very subtle error. What you are seeing is a consequence of there being no negative integer literals in C++. If we look at [lex.icon] we get that a integer-literal,


整数-literal

         decimal-literal integer-suffix opt

         [...]

integer-literal
        decimal-literal integer-suffixopt
        [...]

可以是十进制文字


decimal-literal:

         nonero-digit

          decimal-literal' opt 数字

decimal-literal:
        nonzero-digit
        decimal-literal ’ opt digit

其中数字 [0-9] 非零数字 [1-9] 和后缀par可以是 u U l之一 L ll LL 。这里没有任何地方包括 - 作为十进制文字的一部分。

where digit is [0-9] and nonzero-digit is [1-9] and the suffix par can be one of u, U, l, L, ll, or LL. Nowhere in here does it include - as being part of the decimal literal.

在§2.13.2中,我们还有:

In §2.13.2, we also have:


整数文字是一个没有句号或指数部分的数字序列,可选分隔在确定其值时忽略的单引号。整数文字可以具有指定其基数的前缀和指定其类型的后缀。数字序列的词汇第一个数字是最重要的。 十进制整数文字(十进制)以0以外的数字开头,由一系列十进制数字组成。

An integer literal is a sequence of digits that has no period or exponent part, with optional separating single quotes that are ignored when determining its value. An integer literal may have a prefix that specifies its base and a suffix that specifies its type. The lexically first digit of the sequence of digits is the most significant. A decimal integer literal (base ten) begins with a digit other than 0 and consists of a sequence of decimal digits.

(强调我的)

这意味着 - in -2147483648 是一元运算符 - 。这意味着 -2147483648 实际上被视为 -1 *(2147483648)。由于 2147483648 对于 int 来说太多了,所以它被提升为 long int 并且歧义来自不匹配。

Which means the - in -2147483648 is the unary operator -. That means -2147483648 is actually treated as -1 * (2147483648). Since 2147483648 is one too many for your int it is promoted to a long int and the ambiguity comes from that not matching.

如果您想以便携方式获取某个类型的最小值或最大值,您可以使用:

If you want to get the minimum or maximum value for a type in a portable manner you can use:

std::numeric_limits<type>::min();  // or max()

这篇关于为什么最负的int值会导致有关模糊函数重载的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆