有时我想从模板外部访问模板参数的类型。为此,我按如下方式输入参数:
template<typename Vector>
class SomeAlgorithm
{
public:
typedef Vector Vector;
// ...
}
我觉得这很方便,因为我可以在客户端代码中使用相同的名称;这很明显也很简单。
template<typename A>
void ComputeSomething(const A& a)
{
typedef typename A::Vector Vector;
Vector v = ...
}
但是,适用于 C++ 的 ReSharper 会发出警告:
typedef redeclares a template parameter from an outer scope.
typedefing 类型参数有什么不好的副作用吗?或者除了为 typedef 使用另一个名称之外,是否有更好的方法来实现相同的目的?
最佳答案
Are there any bad side effects for typedefing the type parameters?
是的,它是无效的。参数名称已经是模板范围内的 typedef 名称,因此您不能声明另一个具有相同名称的 typedef。
Or is there a better way to achieve the same outside of using another name for the typedef?
没有。为 typedef 使用另一个名称。
关于c++ - 创建从外部范围重新声明模板参数的 typedef 的负面影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26407703/