typedef和using之间有什么区别

typedef和using之间有什么区别

本文介绍了typedef和using之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

之间的区别是什么?

  typedef Some :: Nested :: Namespace :: TypeName TypeName; 

  using Some :: Nested :: Namespace :: TypeName; 

以在本地提供缩写 TypeName 范围?

解决方案

它们有不同的起源和不同的用途。






typedef 来自 C :回忆C语言struct is:

  typedef struct _MyStruct {....} MyStruct; 

它允许您为类型引入别名。它可以用于函数的类型,使用尴尬的语法...

  typedef void(* Func) ,Bar); 

其中 Func 现在是指向函数通过复制两个参数(分别为 Foo Bar )并不返回任何内容。






使用原来具有不同的含义。它意味着在范围中注入一个名称。可以注入任何名称(近乎):类型,函数,变量(但不是枚举值...)



使用C ++ 11,语法已增强allow template aliasing:

  template< typename T> 
using equiv_map = std :: map< T,T> ;;
表示混叠






此C ++ 11更改是一个清楚的方向语法协调。注意别名的定义现在类似于变量的定义:

 < name> =< expression> ;;不幸的是,似乎标准保留这种别名模板的情况,所以现在的<$ <$> 

c $ c> typedef
使用共存,每个都有自己的狩猎地。


What are the differences between using

typedef Some::Nested::Namespace::TypeName TypeName;

or

using Some::Nested::Namespace::TypeName;

to provide the shorthand TypeName in the local scope?

解决方案

They have different origins and different uses.


typedef comes from C: recall that the C way to declare a struct is:

typedef struct _MyStruct { .... } MyStruct;

It allows you to introduce an alias for a type only. It can be used for the type of a function, with an awkward syntax...

typedef void (*Func)(Foo, Bar);

Where Func is now a pointer to a function taking two arguments by copy (of types Foo and Bar respectively) and returning nothing.


using has, originally, a different meaning. It is meant to inject a name into a scope. Any name (nearly) can be injected: types, functions, variables (but not enum values...)

With C++11, the syntax has been enhanced to allow template aliasing:

template <typename T>
using equiv_map = std::map<T,T>;

This powered-up using means that aliasing (see below) is now possible, on top of the previous functionalities.


This C++11 change is a clear direction toward syntax harmonization. Note how the definition of an alias is now similar to the definition of a variable:

<name> = <expression>;

这篇关于typedef和using之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:24