Search code examples
c++namespacesusing-declaration

In C++ can one rename/alias a non-namespace, non-class name?


I have

namespace src {
   struct src_bar;
   void src_baz();
   template<class T> class src_qux;
}

which I'd like to reference as

namespace dst {
    struct dst_bar;
    void dst_baz();
    template<class T> class dst_qux;
}

meaning that I'd like to "rename" or "alias" or "relabel" names from src.

For dst_bar one can of course use namespace dst { typedef src_bar dst_bar; }. Is there some (non-macro) equivalent allowing me to rename src::src_baz as dst::dst_baz and src::src_qux as dst::dst_qux?

Unless I'm mistaken, neither a using statement nor a namespace alias can accomplish the other two. Writing templated forwarding functions for dst_baz() is a possibility but requires knowledge of the arity of src_baz. Template typedefs could have dst_qux<T>::type be src_qux<T> but the indirection adds verbosity.

FWIW, my use case is taking C names like somepackage_someportion_somefuncA and providing a namespaced-friendly version somepackage::someportion::somefuncA so that other folks can employ using somepackage::someportion for brevity.


Solution

  • For functions you will have to manually forward the requests. For non-template types you can just typedef. For template types, y can use the new using feature of c++11 To create a typedef-style alias for a template, if your compiler supports it, or else you are basically out of luck.