简体   繁体   English

C ++ 11,使用vs typedef,模板化

[英]C++11, using vs typedef, templatized

Instead of 代替

typedef struct
{
 double x,y;
} Point;

C++11 supports C ++ 11支持

using Point = struct {double x, y;};

Unfortunately, this approach does not work for the type T 不幸的是,这种方法不适用于T型

template <typename T>
using Point = struct {T x, y;};

Is there any way of resolving the problem? 有什么办法解决问题吗?

Is there a reason as to why you want to use a type alias in this case? 为什么要在这种情况下使用类型别名,是否有原因? If it's to avoid prepending the struct keyword, this is not the case in C++. 如果要避免在struct关键字前加注,则在C ++中不是这种情况。

You would just define them directly: struct Point { double x, y; }; 您只需直接定义它们: struct Point { double x, y; }; struct Point { double x, y; }; .

Same with the template case: 与模板情况相同:

template <typename T>
struct Point { T x, y; };

using Point = ...; is a type alias (formally called just 'alias'). 是类型别名(通常仅称为“别名”)。 Ie, it's just a different syntax for typedef where the type involved is named in ... . 即,对于typedef只是一种不同的语法,其中涉及的类型在...命名。

template<typename T> using Point = ...; is an alias template , where the type involved is again named in ... . 是别名模板 ,其中涉及的类型再次在...命名。

What both aliases and alias templates have in common is that both must refer to a type-id (C++11 [basic.scope.pdecl]p3). 别名和别名模板的共同点是两者都必须引用类型ID (C ++ 11 [basic.scope.pdecl] p3)。 Type-ids must in turn name types . 类型标识必须依次命名为type (Go figure.) (去搞清楚。)

The problem is that template<typename T> struct {T x, y;} is not a type, but a class template, and as just established alias templates must refer to types. 问题是, template<typename T> struct {T x, y;}不是一个类型,但类模板,并且如刚刚建立别名模板必须引用类型。 As to changing your code to resolve your problem, I've no idea as you haven't said what it is... ;-] Regarding that, please see What is the XY problem? 至于改变你的代码,以解决您的问题,我不知道你有没有说什么它是...; - ]至于原因,请参见什么是XY问题?

I somewhat disagree with the language lawyering in other answers, so I've decided to write my own. 我在其他答案中与语言律师有些不同,所以我决定自己写。

In short, you cannot use template <typename T> using Point = struct {T x, y;}; 简而言之,您不能通过template <typename T> using Point = struct {T x, y;};使用template <typename T> using Point = struct {T x, y;}; for the same reason you cannot use template <typename T> struct {T x, y;}; 由于相同的原因,您不能使用template <typename T> struct {T x, y;}; .

Alias template is still a template. 别名模板仍然是模板。 And although the standard doesn't state this explicitly, it implies that a template should have a name (see [temp]/4 - A template name has linkage ... ). 并且尽管该标准没有明确说明,但它暗示模板应具有名称(请参阅[temp] / 4- 模板名称具有链接... )。 Which you don't provide. 您没有提供的。

So no, you cannot "resolve the problem" without providing a name. 因此,不,您不能不提供名称就“解决问题”。 But you also cannot just throw a name in an alias-declaration like this: template <typename T> using Point = struct P {T x, y;}; 但是,您也不能只在别名声明中像这样抛出名称: template <typename T> using Point = struct P {T x, y;}; . The standard explicitly forbids it ([dcl.typedef]/2 - The defining-type-specifier-seq of the defining-type-id shall not define a class or enumeration ). 该标准明确禁止使用它([dcl.typedef] / 2- 定义类型标识的定义类型说明符seq不应定义类或枚举 )。

The only thing you can do will look something like this: 您唯一可以做的就是这样:

template <typename T>
struct Point {T x, y;};
template <typename T>
using PointAlias = Point<T>;

Which, as you can see, is pointless (unless, of course, your Point takes several template arguments; in this case, you can make a nifty alias template). 如您所见,这毫无意义(当然,除非您的Point带有几个模板参数;在这种情况下,您可以创建一个漂亮的别名模板)。

So stick with simple template <typename T> struct Point {T x, y;}; 因此,请坚持使用简单的template <typename T> struct Point {T x, y;}; . It does exactly the same thing as your "proposed" syntax. 它与“建议的”语法完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM