Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static import in C++11 (e.g. an enum class)

My usage of enum class (VS2012):

class matrix {
public:
    enum class operation_type {ADD, MULT};
    matrix(operation_type op);
...
}

and in another fragment I use

matrix* m = new matrix(matrix::operation_type::ADD);

If the names are long, this becomes very messy.

Is it possible to somehow import the enum values so that I could write:

matrix* m = new matrix(ADD);

The same regards nested classes - can I import them?

like image 887
Joshua MN Avatar asked Mar 16 '13 13:03

Joshua MN


1 Answers

No, it is not possible.

You cannot omit the operation_type part, because you have made this a scoped enumeration (and that is what scoped enumeration are all about). If you want to avoid it, you have to make it an unscoped enum (removing the class keyword).

Besides, outside of matrix you cannot import a member name through a using declaration as if matrix was a namespace. Moreover, per Paragraph 7.3.3/7 of the C++11 Standard:

A using-declaration shall not name a scoped enumerator.

like image 111
Andy Prowl Avatar answered Oct 29 '22 16:10

Andy Prowl