При изучении некоторых новых функций 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 ++.