1

Is there a way to define a class that can be initialized like how enum does?

For example:

class TYPE;
TYPE _type {
 a,
 b,
 c,
};

_type::a;
TYPE::TYPENAME tn = _type::c; // or such similar things
_type.get_size(); // the example returns 3

Is this kind of definition of class available in c++? If yes, would you please explain the method and how it works? If not, then I might use variadic templates for the implementation.

EDIT

If I made a class as I like, then the below code would work:

main.cpp

#include <iostream>
#include "advanced_enum_class_name_TYPE.h"
TYPE type { a, b, c }; // a, b, c have not been defined before this line
int main() {
    type _type = type::a;
    std::cout << type.get_size() << std::endl; // 3
    if(_type == type::b) {                     // false
        std::cout << "it is b!" << std::endl;
    }
    else {
        std::cout << "it is not b!" << std::endl;
    }
}
VLL
  • 9,634
  • 1
  • 29
  • 54
user5876164
  • 471
  • 3
  • 15
  • Read factory design pattern, it could be the closest answer in my opinion. – seccpur Mar 14 '19 at 04:56
  • Your snippet is incoherent, types and expressions are mixed together. There is no such syntax, and I don't understand your use case. – Passer By Mar 14 '19 at 04:56
  • @PasserBy I changed the one that might be annoying: `typename` to `TYPENAME`. I was wondering if I could improve `enum` with making a `class`, without big changes in syntax. Is this code going to fail however I implement `TYPE`? – user5876164 Mar 14 '19 at 05:06
  • `TYPE _type { a, b, c };` looks like a simple construction/initialization. Of course, it can be done - defining an appropriate constructor. Though, you do declare the `class TYPE` prior? This is necessary to use it for instancing. (A forward declaration like in exposed code is not sufficient for this.) Btw. writing the code horizontally or vertically is irrelevant in C++. C++ has a format free syntax i.e. you can insert an arbitrary number of spaces between tokens where ` ` counts as space as well as newline without changing the meaning. (Of course, preprocessor directives are an exception.) – Scheff's Cat Mar 14 '19 at 07:19
  • @Scheff I could just remember of declaration of struct in c after looking at your comment. But by using such declaration, can we use the class just like enum? For example like `TYPE _type { a, b, c };` or `TYPE __type { a, b, c, d };`, and use it like `_type::b` or `__type::d`. Also, the declaration of `TYPE` in the question is to give some declaration of the class; the definition of the class `TYPE` is given somewhere of course. – user5876164 Mar 14 '19 at 08:20
  • `class` and `struct` are rather the same. The precise difference is that members of `struct` are by default `public`, members of `class` by default `private`. And, of course, you can make things explicitly `public`, `protected`, or `private` in both. – Scheff's Cat Mar 14 '19 at 08:25
  • @Scheff of course they are similar, but is such declaration possible? What I want is to give arbitrary(probably up to MAX_INT) amount of (non-type and non-value; not declared in any ways before as we use in `enum`)names in the bracket, and may use the name like `_type::name`? – user5876164 Mar 14 '19 at 08:32
  • Initialization is a wide topic - nothing I dare to sketch in short in a comment. However, have a look at [Initialization](https://en.cppreference.com/w/cpp/language/initialization) and you will find multiple ways to get the initialization you intend. – Scheff's Cat Mar 14 '19 at 08:32
  • _non-type and non-value_ Huhh? What then? I'm afraid I mis-understood what you actually want. – Scheff's Cat Mar 14 '19 at 08:33
  • @Scheff In `TYPE _type {a, b, c}`, `a`, `b`, `c` are not declared before; they are just names used for `TYPE` just as how `enum` does. – user5876164 Mar 14 '19 at 08:36
  • Names for what? (If you need just names, either use an `enum` (compile time evaluated, no storage at run-time) or use a `std::vector` (storage at run-time)). Sounds like an X-Y problem. May be, you edit your question and try to describe what you actually want to achieve. – Scheff's Cat Mar 14 '19 at 08:39
  • 1
    You can use Boost.Enum for this. See this answer for details: https://stackoverflow.com/a/439004/2527795 – VLL Mar 14 '19 at 09:09
  • @Ville-Valtteri how does boost implement that? – user5876164 Mar 14 '19 at 09:10
  • @user5876164 I don't know, but you can download the code and see for yourself. – VLL Mar 14 '19 at 09:11

0 Answers0