Thursday, November 9, 2017

Digest for comp.lang.c++@googlegroups.com - 6 updates in 2 topics

JiiPee <no@notvalid.com>: Nov 09 07:41PM

Is the a way to initialize enum values so that the end result would be:
 
enum class MyOpt {opt1=1, opt2=2, opt3=4, opt4=8}; // etc....multiplied by 2
 
Yes I can do it manually, but I would like to do it automatically maybe
with a function or something (reducing human error).
 
Or the only way to do it is to hardcode like that?
 
I need this in my flags system where I need values 1,2,4,8,16,...
"James R. Kuyper" <jameskuyper@verizon.net>: Nov 09 03:07PM -0500

On 2017-11-09 14:41, JiiPee wrote:
> Is the a way to initialize enum values so that the end result would be:
 
> enum class MyOpt {opt1=1, opt2=2, opt3=4, opt4=8}; // etc....multiplied by 2
> Yes I can do it manually, but I would like to do it automatically maybe
 
This is about as close as you can get to doing it automatically:
 
enum class MyOpt {opt1=1, opt2, opt3=opt2*2, opt4=opt3*2};
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 09 09:20PM +0100

On 11/9/2017 8:41 PM, JiiPee wrote:
> with a function or something (reducing human error).
 
> Or the only way to do it is to hardcode like that?
 
> I need this in my flags system where I need values 1,2,4,8,16,...
 
Two possible alternatives:
 
* Define `operator|` that adds the necessary `1<<n`.
* Use a `std::bitset`.
 
Cheers & hth.,
 
- Alf
JiiPee <no@notvalid.com>: Nov 09 08:23PM

Thanks, but just thinking another thing: is is a good idea at all using
enums as (option) flags? I read some say online that should use
constants instead.
 
On 09/11/2017 20:20, Alf P. Steinbach wrote:
Ian Collins <ian-news@hotmail.com>: Nov 10 09:28AM +1300

On 11/10/2017 09:23 AM, JiiPee wrote:
> Thanks, but just thinking another thing: is is a good idea at all using
> enums as (option) flags? I read some say online that should use
> constants instead.
 
The arguments are less clear now that with have typed enums! Before
C++11 a good case could be made for constants given enums always had an
underlying type of int. Now we can specify the type, I think enums are
a better choice.
 
--
Ian.
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Nov 09 05:37AM -0800

Christian:
 
You know judgment for sin looms over every human soul, yet you remain
silent? You do not actively warn the people of their ABSOLUTE NEED to
come to Jesus and ask forgiveness for sin?
 
How much do you hate people to leave them ignorantly on the road to Hell?
 
You should be ashamed of yourself!
 
--
Rick C. Hodgin
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: