在枚举类上使用`reinterpret_cast`-有效或未定义的行为? [英] Using `reinterpret_cast` on an enum class - valid or undefined behavior?

查看:103
本文介绍了在枚举类上使用`reinterpret_cast`-有效或未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <cassert>
#include <type_traits>

template<typename T> using Underlying = std::underlying_type_t<T>;

enum class ETest : int
{
    Zero = 0,
    One = 1,
    Two = 2
};

template<typename T> auto& castEnum(T& mX) noexcept
{
    // `static_cast` does not compile
    // return static_cast<Underlying<T>&>(mX);

    return reinterpret_cast<Underlying<T>&>(mX);
}

int main()
{
    auto x(ETest::Zero);
    castEnum(x) = 1;
    assert(x == ETest::One);

    return 0;
}

ideone

是否可以保证此代码始终有效?还是未定义的行为?

Is this code guaranteed to always work? Or is it undefined behavior?

推荐答案

标准有点不清楚:


3.10左值和右值[basic.lval]

10如果程序尝试通过以下值访问对象的存储值:除了以下类型之一以外,行为是不确定的:

10 If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:

[...]

(10.4)-一种带符号的类型或对应于对象动态类型的无符号类型,

(10.4) -- a type that is the signed or unsigned type corresponding to the dynamic type of the object,

[...]

合法地读为是说,与枚举类型相对应的有符号或无符号类型是其基础类型,但是我认为这旨在涵盖通过它们的其他有符号对应类型来 only 访问整数类型,即枚举类型的基础类型不算作与该枚举类型相对应的(未签名)类型。

This could be legitimately read as saying that the signed or unsigned type corresponding to an enumeration type is its underlying type, but I'd think this is meant to cover only accessing integer types through their other-signed corresponding type, that the underlying type of an enumeration type does not count as the (un)signed type corresponding to that enumeration type.

至少GCC对此表示同意:它会为

At least GCC agrees with this: it gives an aliasing warning for

enum E : int { };
int f(E e) { return *(int *) &e; }



warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

强烈暗示它将在程序中没有此类别名的前提下进行优化。

strongly hinting that it will optimise on the assumption that no such aliasing takes place in your program.

这篇关于在枚举类上使用`reinterpret_cast`-有效或未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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