Avoid repeating template parameter to access enumeration in class template

Consider a class template that implements a container that includes the ability to select a place to store.

template<class T>
class Container {
public:
  enum StorageOption {A,B};
  Container(StorageOption opt_): option(opt_) {}
private:
  StorageOption option;
};

Here StorageOptionwas selected as a member, since it is used only in the class.

Now, to create an instance of the class, I will need to repeat the template parameter, for example:

{
  Container<int> c( Container<int>::A );
}

Is there a way to avoid repeating the parameter and at the same time have StorageOptiona member, or is there a better way to implement the option?

+5
source share
2 answers

This is usually achieved by defining it in the base class.

class ContainerBase {
public:
  enum StorageOption {A,B};
};


template<class T>
class Container : public ContainerBase{
public:
  Container(StorageOption opt_): option(opt_) {}
private:
  StorageOption option;
};

Container<int> c( ContainerBase::A );
+7
source

, , , enum. :

namespace FuncEnum{
    enum class FuncNeighbor{
        FLOOR, CEILING, SELF
    };
    enum class FuncDirection{
        BACK, FRONT, BACKNFRONT
    };
    enum class FuncVar{
        X, Y
    };
} using namespace FuncEnum;

template<typename... Types>
class Func {};

, enum . , < class nickname > < enum class name > , , (?) - , ( ).

0

All Articles