Search code examples
templatesusingc++03

Using clause on template class


To make referencing types easier you can bring individual types into the current scope with the using clause:

namespace MyCompany
{
    namespace MyProject
    {
        class MyType {};
        void myFunc(MyType const& obj) {}
    }
}

int main()
{
    using MyCompany::MyProject::MyType;
    using MyCompany::MyProject::myFunc;

    MyType  plop;
    myFunc(plop);
}

The problem is if the class is a template class (or function). Is it possible to bring these into the current scope without fully instantiating them?

namespace MyCompany
{
    namespace MyProject
    {
        template<typename T>
        class MyType {};

        template<typename T>
        void myFunc(MyType<T> const& obj) {}
    }
}

int main()
{
    // Can I bring the templates into scope?
    // Or do I need to fully instantiate each version in the using clause?
    using MyCompany::MyProject::MyType;  ????
    using MyCompany::MyProject::myFunc;  ????

    MyType  plop;
    myFunc(plop);
}

Solution

  • Both VC10 & gcc run the code ok with different instantiations after the using declaration. So it seems the using declaration has brought the whole template to the scope.

    Also in c++0x standard n3290 7.3.3 - 5

    A using-declaration shall not name a template-id. [ Example:
    struct A {
     template <class T> void f(T);
     template <class T> struct X { };
    };
    struct B : A {
     using A::f<double>; // ill-formed
     using A::X<int>; // ill-formed
    };
    

    which seems to suggest that using should not be used with specific template-id.