代替
typedef struct
{
double x,y;
} Point;
C++11 支持
using Point = struct {double x, y;};
不幸的是,这种方法不适用于类型 T
template <typename T>
using Point = struct {T x, y;};
有什么办法可以解决这个问题吗?
最佳答案
using Point = ...;
是类型别名(正式名称为“别名”)。即,它只是 typedef
的不同语法其中涉及的类型在 ...
中命名.
template<typename T> using Point = ...;
是别名 template,其中涉及的类型再次在 ...
中命名.
别名和别名模板的共同点是都必须引用type-id (C++11 [basic.scope.pdecl]p3)。 Type-id 必须依次命名 types。 (去图。)
问题是 template<typename T> struct {T x, y;}
不是类型,而是类模板,作为刚刚建立的别名模板必须引用类型。至于更改您的代码以解决您的 问题,我不知道,因为您还没有说它是什么...;-] 关于此,请参阅 What is the XY problem?
关于C++11,使用 vs typedef,模板化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38405704/