c++ - 创建从外部范围重新声明模板参数的 typedef 的负面影响?

标签 c++ templates typedef

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

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/

相关文章:

c++ - 在 unordered_map 中插入字符串键、值时出错

c++ - 具有固定步长的 ode 求解器

c++ - 使用 if 语句选择类型来声明变量 C++

c++ - 使用模板元编程计数?

c++ - typedef 类型和相同类型的指针的正确方法是什么?

c - 为什么 C struct tm 不是 typedef?

C++理解cocos2d-x函数指针的使用

c++ - 如何使用 cytpes 将 int 列表的列表从 python 传递给 C++ 函数

templates - 动态地将属性插入 JSF2/Primefaces 模板中?

c++ - 如何根据模板参数返回不同的类型