什么是“auto var = {condition}”的类型? 1:1.0“在C ++ 11?是double还是int? [英] What is the type of "auto var = {condition} ? 1 : 1.0" in C++11? Is it double or int?

查看:208
本文介绍了什么是“auto var = {condition}”的类型? 1:1.0“在C ++ 11?是double还是int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,当我写这个时, x y 的类型是什么? >

In C++11 what are the types of x and y when I write this?

int main()
{
    auto x = true ? 1 : 1.0;
    auto y = false ? 1 : 1.0;
    std::cout << x << endl;
    std::cout << y << endl;
    return 0;
}


推荐答案

类型将是 double ,因为它是文字的常见类型 1 1.0

The type is going to be double, because it's the common type of the literals 1 and 1.0.

测试使用 typeid

#include <iostream>
#include <typeinfo>
using namespace std;

int main() {
    auto x = true ? 1 : 1.0;
    cout << typeid(x).name() << endl;
    return 0;
}

这会输出 d 对我的版本的GCC。
运行 echo d | c ++ filt -t 然后告诉我们 d 对应于 double 如预期。

This outputs d on my version of GCC. Running echo d | c++filt -t then tells us that d corresponds to the type double, as expected.

这篇关于什么是“auto var = {condition}”的类型? 1:1.0“在C ++ 11?是double还是int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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