C ++-将枚举类作为值或const引用传递更好吗? [英] C++ - Is it better to pass a enum class as value or const reference?

查看:43
本文介绍了C ++-将枚举类作为值或const引用传递更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为它被称为类",所以我通常将其作为const引用传递,但是如果我使用普通的枚举,则没有区别吧?那么,如果我将枚举类作为value/const ref传递,会有所不同吗?另外,类型重要吗?例如一个枚举类:int

Since its called a 'class' I would usually pass it as const reference, but if I use a plain enum it makes no difference right? So does it make a difference if I pass a enum class as value/const ref? Also, does the type matter? For example a enum class:int

推荐答案

枚举类与常规 enum 一样,具有整数值,因此您可以按值安全地传递它没有任何开销.请注意,编译器有时可能还会通过将值替换为按值传递来优化按引用传递.但是,如果不应用这种优化,则按引用传递可能会导致一些开销.

enum class holds an integral value just like a regular enum so you can safely pass it by value without any overhead. Notice that compiler may sometimes optimize pass by reference as well by replacing it with pass by value. But passing by reference may result in some overhead when such an optimization is not applied.

这篇关于C ++-将枚举类作为值或const引用传递更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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