gpt4 book ai didi

c++ - 与条件运算符一起使用时 decltype 行为不一致

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:32 25 4
gpt4 key购买 nike

在研究一些新的 C++11 特性时,我观察到一些与新的 decltype 关键字及其与条件运算符的交互相关的奇怪之处。

我很惊讶地看到下面程序的输出:

#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;
}

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

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

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

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++。

最佳答案

你需要改变你的期望。条件表达式的类型仅取决于其操作数的类型,而非其操作数的值。

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

您应该查阅标准以了解确定公共(public)类型的规则的详细信息。如果找不到通用类型,则程序通常是病式的。

关于c++ - 与条件运算符一起使用时 decltype 行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7746029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com