Templates 在模板类上使用子句

Templates 在模板类上使用子句,templates,using,c++03,Templates,Using,C++03,要使引用类型更容易,可以使用using子句将单个类型引入当前范围: namespace MyCompany { namespace MyProject { class MyType {}; void myFunc(MyType const& obj) {} } } int main() { using MyCompany::MyProject::MyType; using MyCompany::MyProject:

要使引用类型更容易,可以使用using子句将单个类型引入当前范围:

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);
}
问题在于该类是否为模板类(或函数)。是否可以在不完全实例化它们的情况下将它们引入当前范围

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);
}
名称空间MyCompany
{
名称空间MyProject
{
模板
类MyType{};
模板
void myFunc(MyType const&obj){}
}
}
int main()
{
//我可以将模板引入范围吗?
//或者我需要在using子句中完全实例化每个版本吗?
使用MyCompany::MyProject::MyType????
使用MyCompany::MyProject::myFunc????
MyType-plop;
myFunc(plop);
}

VC10和gcc在using声明之后都会使用不同的实例化运行代码ok。 因此,使用声明似乎将整个模板带到了范围内

同样在c++0x标准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
};
使用声明不得命名模板id。[示例:
结构A{
模板空隙f(T);
模板结构X{};
};
结构B:A{
使用A::f;//格式错误
使用A::X;//格式错误
};

这似乎表明使用不应与特定的模板id一起使用。

您需要使用名称空间中的所有类型还是仅使用一些类型?我不知道这个问题的答案,但您可以通过使用名称空间MyProject来妥协;一个好的答案取决于您是否可以使用C++11(因为C++11引入了模板别名)。我仍然坚持使用C++03,但我相信C++11的答案对其他人会有用,所以请添加它们。