带模板功能的Typedef [英] Typedef with template functions
问题描述
说我在命名空间A中有一个模板函数。我还有另一个命名空间B。在命名空间A中声明了一个模板函数,定义为
Say I have a template function in namespace A. I also have another namespace B. There is a template function declared in namespace A, which is defined as
template<typename T, typename U>
void f(T a, U b);
现在在命名空间B中,我想声明一种特殊类型的模板函数。我在想是否可以 typedef
模板函数,以便在命名空间B中将其声明为
Now in namespace B, I would want to declare a specialized type of the template function. I was thinking if I could typedef
the template function so it is declared in namespace B as
void f(int a, double b);
实际上没有实现调用模板函数的函数。由于有一种方法可以使用特定的模板参数声明新的类型名,难道不应该有一种方法也可以通过函数来做到这一点吗?
我尝试了不同的方法来实现它,但是并没有完全解决问题。
without actually implementing the function calling the template function. As there is a way to declare new typenames with specific template parameters, shouldn't there be a way to do that with functions aswell? I tried different methods to achieve it, but it didn't quite work out.
所以C ++中已经存在一种用给定的函数重新声明函数的方法。模板参数而没有实际实现新功能?如果不是这样,它可以在C ++ 11中实现吗?
So is there already a way in C++ to redeclare the function with given template parameters without actually implementing a new function? If not, is it somehow achievable in C++11?
这将是一个简单的功能,因为它将使功能的目的更加清楚,并且语法上更好:)
It would be a neat feature to have since it would make the purpose of the function more clear and would be syntactically better :)
编辑:所以可以这样写:
So one could write:
using A::f<int, double>;
在B名称空间中,该函数将显示这些模板参数
in B namespace and the function would show up with those template parameters
推荐答案
您可以使用使用
:
namespace A {
template <typename T> void f(T);
template <> void f<int>(int); // specialization
}
namespace B {
using ::A::f;
}
您无法区分专业化 (因为使用
仅与名称有关),但是应该足以使所需的专业化可见。
You can't distinguish between the specializations like that (since using
is only about names), but it should be enough to make the desired specialization visible.
这篇关于带模板功能的Typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!