views:

73

answers:

2

Suppose I have

 template< unsigned int num >
 class SomeFunctionality
 {
     static unsigned int DoSomething()
     {
         //...
     }

     static void DoSomethingElse()
     {
     }
 };

 typedef SomeFunctionality<6> SomeFunctionalityFor6;

Semantically, "SomeFunctionalityFor6" is essentially a namespace specific to the template argument, 6. So in the code using this instance of the template instead of doing

 int main()
 {
       SomeFunctionalityFor6::DoSomething();
 }

I'd rather have the ability to use a "using" statement ala a real namespace

 int main()
 {
       using SomeFunctionalityFor6;
       DoSomething();
 }

This, as I would suspect doesn't work. Visual studio complains that it wants a namespace defined by the "namespace" keyword following any using statement.

Is there anyway to do what I'm trying to do? Mainly I just don't want to fully qualify the namespace everytime I call the static methods. I know its mostly just syntactic sugar, but in my opinion it can make code much more readable. I'm wondering if there's even ways to templatize a namespace directly instead of having to use the "class" keyword.

+1  A: 

You can't do that. Neither templatized namespace, nor using class_name.

The only places in the code that can use static functions from a class without qualification are derived classes.

In your case, I would use a typedef for some short name, like

int main()
{
       typedef SomeFunctionalityFor6 SF6;
       SF6::DoSomething();
}
jpalecek
+1  A: 

Or you could just create a local object...

int main()
{
  SomeFunctionalityFor6  SF6;
  SF6.DoSomething();
}

You could replace/change the SF6 object at will.

Mr.Ree
Hmm good point, your answer made me think about whether I wanted this particular case to be something I'd want there to be particular instances of, but decided that in this case I'd rather treat it like a bunch of free functions in a namespace.
Doug T.