加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在MSVC中缩小转换为bool警告

发布时间:2020-12-16 07:24:30 所属栏目:百科 来源:网络整理
导读:编译此代码时: enum B: bool { T = true };struct A { bool member; };void foo(const B b = T){ A a{b}; // warning here}void bar(){ const B b = T; A a{b};} MSVC在foo中发出警告: warning C4838: conversion from ‘const B’ to ‘bool’ requires a
编译此代码时:

enum B: bool { T = true };
struct A { bool member; };

void foo(const B b = T)
{
    A a{b}; // warning here
}

void bar()
{
    const B b = T;
    A a{b};
}

MSVC在foo中发出警告:

warning C4838: conversion from ‘const B’ to ‘bool’ requires a narrowing conversion

但编译条很好.

这是一个proof

它是编译器错误还是预期的行为?

解决方法

narrowing conversion定义的相关部分在C 17 [dcl.init.list] / 7中:

A narrowing conversion is an implicit conversion:

  • […]
  • from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type,except where the source is a constant expression whose value after integral promotions will fit into the target type.

在你的代码中,B是一个带有固定底层类型bool的无范围枚举.在[dcl.enum] / 8中它说:

For an enumeration whose underlying type is fixed,the values of the enumeration are the values of the underlying type

这意味着B的唯一可能值是bool的值,即true和false.它不能容纳其他价值观.

由于A :: member实际上可以表示B的所有值,因此它不是缩小转换,因此警告是假的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读