3

There are several questions on these forums about the inheritance of C++ enums for extending (which is actually the thing without the logic). But what about inheritance just for setting specific values? Currently, there is something like the following in my code:

//lib_impl.h
enum class X {
    a = 13, // these values are
    b = 42  // implementation dependent
}

//lib.h
#include "lib_impl.h"

void some_func(X param) {
    X x = X::a;
}

I just want to avoid the dependecy of the 'lib' from its implementation. Probably, something other than enums must be used for that. As even in C++11 we have the ability only to declare forward enum name, but not its enumerators:

//lib.h
enum class X { a, b } // this is both declaration and definition, unfortunately

void some_func(X param) {
    X x = X::a;
}

//lib_impl.h
#include "lib.h"

enum class X { // redefenition, compilation error
    a = 13,
    b = 42 
}

What is the best compile-time solution for such problems?

--

As it seems to be unimplementable in c++, what is the most common way to resolve such issues? Leave the dependency of the 'lib' from the 'impl' as it is? Probably, 'impl' could be split into two parts, small which will be included before the 'lib.h' and other, bigger, to be included after it. Is it ok or I need to abandon the use of enums in favor of abstract classes?

3
  • 1
    How is the compiler supposed to generate code for some_func? Either X::a is a compile-time constant, in which case the compiler needs to see actual value, one way or another. Or else it's a variable occupying space, in which case the compiler would generate code for loading the value from that space. You can achieve the latter my making X an actual class, and a its static member. Commented Nov 24, 2013 at 18:30
  • Static functions cannot be virtual, can they? Commented Nov 24, 2013 at 18:42
  • 1
    No they cannot be. Though I struggle to see the relevance of this fact to the issue at hand. Commented Nov 24, 2013 at 18:49

1 Answer 1

2

Expose an enum with nominal values (start with 0, sequential say). Inside your library, remap these values to an internal enum with implementation dependent values (say an array for speed, using the external value as index). Reverse the mapping if you export said enum values to the outside (the reverse mapping will be slower).

Sign up to request clarification or add additional context in comments.

2 Comments

It is not compile-time, isn't it?
@Grief no. You want one compilation unit to have one set of values, and another compilation unit to have different values. At compile time, the external compilation unit cannot depend on the values in the internal compilation unit in order to do what you want. So to do what you asked, at run time, you map the external values to internal values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.