using declaration does not seem to work with enum type
class Sample{
public:
enum Colour { RED,BLUE,GREEN};
}
using Sample::Colour;
does not work!! do we need to add using declaration for every enumerators of enum type? like below
using sample::Colour::RED;
C++ Standard, 7.3.3.1:
A class does not define a namespace, therefore "using" isn't applicable here.
Also, you need to make the enum public.
If you're trying to use the enum within the same class, here's an example:
And to access it from without the class:
To add to Stevela's answer, the problem with the original code is that you refer to a member, but the using declaration is not itself a member declaration:
7.3.3/6 has:
To highlight this, the following example does work:
Finally, as pointed out by Igor Semenov here, even if you move the enum definition into a namespace, thereby allowing the using declaration, the using declaration will only declare the name of the enum type into the namespace (The 2003 standard reference is 7.3.3/2).
Dependent Base Types
To allow for partial and explicit specializations, when the compiler parses a class template it does not perform any lookups in dependent base classes. As a result, the following variation with Sample as a template does not compile:
The problem is that
Derived::Colour
is treated as an object by the compiler (14.6/2):Looking at the two conditions for the name to be a type:
Colour
doesn't find a type because the dependent baseSample<T>
is not searched.typename
The example therefore needs the
typename
keyword:Note: The '98 version of the standard didn't allow
typename
to be used with a using declaration and so the above fix was not possible. See Accessing types from dependent base classes and CWG11.