Reputation: 35
template <Type T>
class Socket {
public:
enum class Type {
TCP,
UDP
};
...
}
How I can use enum Type
for class template? I want to use it as Socket::Type::UDP
and etc.
I try declare enum class Socket::Type
before Socket
but it doesn't work.
Upvotes: 3
Views: 394
Reputation: 61920
Each Socket<...>
has a different Type
enum. Apart from the separate enum types, one reason it isn't possible to use Socket::Type
is that a specialization of Socket
could not even include the enum, or make Type
something other than an enum.
You have two main options:
Use a separate class/namespace named closely:
class Sockets {
public:
enum class Type { ... };
};
template<Sockets::Type T>
class Socket { ... };
Socket<Sockets::Type::TCP> s;
Use a separate enum type:
enum class SocketType { ... };
template<SocketType T>
class Socket { ... };
Socket<SocketType::TCP> s;
Alternatively, don't template Socket
. You'll surely end up with a bunch of if (T == TCP) { ... } else { ... }
. It would likely be better to cleanly separate the common parts and use two different implementations for TCP and UDP.
Upvotes: 3