创建从外部作用域重新声明模板参数的typedef的负面影响

Negative side effects of creating a typedef that redeclares a template parameter from an outer scope?

本文关键字:参数 typedef 影响 作用域 从外部 新声明 声明 创建      更新时间:2023-10-16

有时我想从模板外部访问模板参数的类型。为此,我键入如下参数:

template<typename Vector>
class SomeAlgorithm
{
public:
    typedef Vector Vector;
    // ...
}

我觉得这很方便,因为我可以在客户端代码中使用相同的名称;它是显而易见和简单的。

template<typename A>
void ComputeSomething(const A& a)
{
    typedef typename A::Vector Vector; 
    Vector v = ... 
}

然而,ReSharper for C++发出警告:

typedef redeclares a template parameter from an outer scope.

对类型参数进行类型定义有什么不良的副作用吗?或者,除了为typedef使用另一个名称之外,还有更好的方法可以实现同样的效果吗?

对类型参数进行类型定义有什么不良的副作用吗?

是的,它是无效的。参数名称已经是模板作用域中的typedef名称,因此不能使用相同名称声明另一个typedef。

或者,除了为typedef使用另一个名称之外,还有更好的方法可以实现同样的效果吗?

没有。为typedef使用另一个名称。