4

I created small class that allows me to use enumerators of strongly-typed enums as flags (in combination). I'm using type_traits for underlying type detection so it should be also slightly type safe and mostly processed at compile time. However, i was wondering if it is really worth it.

I can now write something like

void Foo(Flags<Mode> Value);

and the programmer will see that he can use only enumerators from Mode (e.g. Mode::Read) and also he can't combine any other enums with Mode. Do you think it is better way than

void Foo(int Mode);

, i'm not sure if people can appreciate it?

HemoGoblin
  • 155
  • 4
  • 5
    What is the performance cost of doing this? Measure it, and you can probably answer your own question... – Eric J. Aug 02 '12 at 15:58
  • 4
    These should optimize into exactly the same code... This is the beauty of C++; you can make tons of compile-time assumptions (the most fundamental being types) which don't incur any run-time overhead. – tenfour Aug 02 '12 at 16:01
  • 1
    @EricJ., the difference (if any) is probably so low that it would be extremely difficult to profile the difference. You'd be better off inspecting the generated assembly code. See http://mortoray.com/2012/07/09/pitfalls-of-performance-testing/ – edA-qa mort-ora-y Aug 02 '12 at 16:03
  • @edA-qamort-ora-y: Exactly :-) – Eric J. Aug 03 '12 at 02:29

1 Answers1

7

What you are suggesting is considered best practice.

With a modern optimizing compiler there should be no performance cost.

Johan Råde
  • 20,480
  • 21
  • 73
  • 110