当与条件运算符结合使用时,decltype表现不一致 [英] decltype acting inconsistent when used in conjunction with conditional operator

查看:95
本文介绍了当与条件运算符结合使用时,decltype表现不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究一些新的C ++ 11功能时,我观察到一些与新的decltype关键字有关的陌生,以及它与条件运算符的交互。

While studying some of the new C++11 features, I observed some strangeness related to the new decltype keyword and its interaction with the conditional operator.

非常惊讶地看到以下程序的输出:

I was very surprised to see the output of the following program:

#include <iostream>
#include <map>

int main(void)
{
    // set up a map that associates the internal compiler-defined type_info name with a human readable name
    std::map <std::string, std::string> types;
    types[typeid(decltype(static_cast<unsigned char  >(0))).name()] = "unsigned char";
    types[typeid(decltype(static_cast<unsigned short >(0))).name()] = "unsigned short";
    types[typeid(decltype(static_cast<short          >(0))).name()] = "short";
    types[typeid(decltype(static_cast<unsigned int   >(0))).name()] = "unsigned int";
    types[typeid(decltype(static_cast<int            >(0))).name()] = "int";
    types[typeid(decltype(static_cast<float          >(0))).name()] = "float";
    types[typeid(decltype(static_cast<double         >(0))).name()] = "double";
    types[typeid(decltype(static_cast<bool           >(0))).name()] = "bool";

    std::cout << "Should be unsigned char : " << types[typeid(decltype(static_cast<unsigned char >(0))).name()] << std::endl;
    std::cout << "Should be unsigned short: " << types[typeid(decltype(static_cast<unsigned short>(0))).name()] << std::endl;
    std::cout << "Should be short         : " << types[typeid(decltype(static_cast<short         >(0))).name()] << std::endl;
    std::cout << "Should be unsigned int  : " << types[typeid(decltype(static_cast<unsigned int  >(0))).name()] << std::endl;
    std::cout << "Should be int           : " << types[typeid(decltype(static_cast<int           >(0))).name()] << std::endl;
    std::cout << "Should be float         : " << types[typeid(decltype(static_cast<float         >(0))).name()] << std::endl;
    std::cout << "Should be double        : " << types[typeid(decltype(static_cast<double        >(0))).name()] << std::endl;

    std::cout << "Expecting unsigned short: " << types[typeid(decltype(
        false ? static_cast<unsigned char  >(0) :
        true  ? static_cast<unsigned short >(0) :
        false ? static_cast<         short >(0) :
        false ? static_cast<unsigned int   >(0) :
        false ? static_cast<         int   >(0) :
        false ? static_cast<         float >(0) :
        false ? static_cast<         double>(0) :
                static_cast<         bool  >(0)
        )).name()] << std::endl;
}

这导致了令人惊讶的输出:

Which resulted in the surprising output:

Should be unsigned char : unsigned char
Should be unsigned short: unsigned short
Should be short         : short
Should be unsigned int  : unsigned int
Should be int           : int
Should be float         : float
Should be double        : double
Expecting unsigned short: double

我会看到以下输出(注意最后一行):

I would have expected to have seen the following output (note the last line):

Should be unsigned char : unsigned char
Should be unsigned short: unsigned short
Should be short         : short
Should be unsigned int  : unsigned int
Should be int           : int
Should be float         : float
Should be double        : double
Expecting unsigned short: unsigned short

有人知道为什么会发生这种情况吗?我使用GNU g ++。

Does anyone know why this might be happening? I am using GNU g++.

推荐答案

你需要改变你的期望。条件表达式的类型只依赖于其操作数的类型,不是其操作数的值。

You need to change your expectation. The type of a conditional expression depends only on the types of it's operands, not the value of its operands.

有许多规则用于从第二和第三操作数的类型确定条件表达式的公共类型。第二个和第三个操作数的值,即使它们是常数表达式也不会被考虑。

There are a number of rules that are used to determine a common type for a conditional expression from the types of the second and third operands. The values of the second and third operands, even if they are constant expressions, are not taken into account.

您应该查阅标准,普通类型。如果找不到通用类型,程序通常是错误的。

You should consult the standard for the details of the rules for determining the common type. If a common type can't be found the program is usually ill-formed.

这篇关于当与条件运算符结合使用时,decltype表现不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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