我为什么要在C ++中使用typedef?

时间:2021-10-04 18:53:00

Lets say I have:

让我们说:

set<int, less<int> > m_intset;

That works, but now I change it to typedef, and I do end up with two lines of code:

这工作,但现在我将其更改为typedef,我最终得到两行代码:

typedef set<int, less<int> > SetInt;
SetInt m_intset;

What's the advantage of such a typedef? Am I using it correctly?

这种typedef有什么优势?我正确使用它吗?

5 个解决方案

#1


18  

Some advantages to using typedef:

使用typedef的一些优点:

  1. Simplification: Now, every time you would normally have needed set<int, less<int> >, you only have to type SetInt. Not a big deal if you're only using the set once, but if you've got multiple instances of the same set, or need to access iterators thereof, the shorter form is much easier to read and type.

    简化:现在,每次你通常需要set >时,你只需要输入SetInt。如果你只使用一次这个集合没什么大不了的,但是如果你有同一个集合的多个实例,或者需要访问它的迭代器,那么更短的形式更容易阅读和输入。 ,less>

  2. Clarification: set<int, less<int> > doesn't tell me anything about what the variable is used for, just that it's a set of ints. With typedef, you can choose an identifier that explains the purpose of the type, such as Inventory or Coordinates, for example.

    澄清:set >不会告诉我关于变量用途的任何信息,只是它是一组int。使用typedef,您可以选择一个解释类型用途的标识符,例如Inventory或Coordinates。 ,less>

  3. Abstraction: Programs change. Although you may think you need a set<int, less<int> > now, there's always the possibility that your requirements may change in the future, necessitating a set<unsigned long, less<unsigned long> > or vector<int> or some other change. By using a typedef, you would only have to fix this in one place, and it will affect every instance of SetInt in the program. This is far simpler and less prone to error than manually changing a dozen or more entries in the source files.

    抽象:程序改变。虽然您可能认为现在需要一个set >,但您的需求总是有可能在将来发生变化,需要设置 >>或vector 或者其他一些变化。通过使用typedef,您只需要在一个地方修复它,它将影响程序中SetInt的每个实例。与手动更改源文件中的十几个条目相比,这更容易并且更不容易出错。 ,less>

Due to points 2 and 3 above, typedef can be very powerful when used inside a class or a library, since it can strengthen the separation between interface and implementation which is generally considered good C++ form.

由于上面的第2点和第3点,当在类或库中使用时,typedef可以非常强大,因为它可以加强接口和实现之间的分离,这通常被认为是良好的C ++形式。

Of course, most of these advantages only really show themselves if you're using the same type multiple times in the same program. If you only expect to use the set<int, less<int> > once, typedef is probably overkill.

当然,如果你在同一个程序中多次使用相同的类型,这些优点中的大多数只能真正显示出来。如果您只希望使用set >一次,则typedef可能过度。 ,less>

#2


5  

Typedef's are mostly notational convenience, but they do have a bunch of benefits:

Typedef主要是符号方便,但它们确实有很多好处:

  • Declaring function pointer types (e.g. for callbacks)--notoriously hard to get right when spelled out completely.
  • 声明函数指针类型(例如,用于回调) - 在完全拼写出来时很难完成。

  • Dependent type information is easier to describe (e.g. SetInt::iterator) and should you ever change your mind (suppose you went to a hash_set instead of a std::set), is effectively updated immediately due to the typedef
  • 依赖类型信息更容易描述(例如SetInt :: iterator),如果你改变主意(假设你去了一个hash_set而不是std :: set),由于typedef而立即有效更新

  • They describe intent better than built-in typenames do (e.g. typedef int ErrCode)
  • 它们比内置类型名更好地描述了intent(例如typedef int ErrCode)

I'm sure there are more I'm not thinking of....

我相信还有更多我没想到......

#3


4  

You have it exactly correct. Using a typedef makes it easier to use things like SetInt::iterator.

你有完全正确的。使用typedef可以更容易地使用SetInt :: iterator之类的东西。

#4


3  

The benefit is when you refer to the same type multiple times. For example, if you want to iterate over your set; compare:

当您多次引用相同类型时,好处是。例如,如果要迭代您的集合;比较:

for (set<int, less<int> >::iterator i = xs.begin(); i != xs.end(); ++i)

with:

for (IntSet::iterator i = xs.begin(); i != xs.end(); ++i)

The difference gets more pronounced if you have nested template parameters (e.g. a set of pair of vector of string).

如果您有嵌套模板参数(例如一组字符串向量),则差异会更明显。

#5


2  

You are creating an alias to a type to be used in a specific context. How about this example:

您正在为要在特定上下文中使用的类型创建别名。这个例子怎么样:

typedef int INT32

That's all well and good, and if you only ever compiled for environments where an int was a 32-bit quantity you would not even need it. However, perhaps, some day in the future, you port your code to a system where a native int is 64 bits. Now your code may have some bugs if it assumes a 32-bit size for int's everywhere they are used. With the typedef, you need only change the argument:

这一切都很好,如果你只编译了int是32位数量的环境,你甚至不需要它。但是,也许在将来的某一天,您将代码移植到本机int为64位的系统中。现在你的代码可能会有一些错误,如果它假定int的32位大小在任何地方使用它们。使用typedef,您只需要更改参数:

typedef /*some 32 bit type*/ INT32 

Note that this is only an example. The idea is abstracting the type away if it may change in the future or clears up your code.

请注意,这只是一个例子。如果将来可能发生变化或清除代码,那么这个想法就会抽象出类型。

#1


18  

Some advantages to using typedef:

使用typedef的一些优点:

  1. Simplification: Now, every time you would normally have needed set<int, less<int> >, you only have to type SetInt. Not a big deal if you're only using the set once, but if you've got multiple instances of the same set, or need to access iterators thereof, the shorter form is much easier to read and type.

    简化:现在,每次你通常需要set >时,你只需要输入SetInt。如果你只使用一次这个集合没什么大不了的,但是如果你有同一个集合的多个实例,或者需要访问它的迭代器,那么更短的形式更容易阅读和输入。 ,less>

  2. Clarification: set<int, less<int> > doesn't tell me anything about what the variable is used for, just that it's a set of ints. With typedef, you can choose an identifier that explains the purpose of the type, such as Inventory or Coordinates, for example.

    澄清:set >不会告诉我关于变量用途的任何信息,只是它是一组int。使用typedef,您可以选择一个解释类型用途的标识符,例如Inventory或Coordinates。 ,less>

  3. Abstraction: Programs change. Although you may think you need a set<int, less<int> > now, there's always the possibility that your requirements may change in the future, necessitating a set<unsigned long, less<unsigned long> > or vector<int> or some other change. By using a typedef, you would only have to fix this in one place, and it will affect every instance of SetInt in the program. This is far simpler and less prone to error than manually changing a dozen or more entries in the source files.

    抽象:程序改变。虽然您可能认为现在需要一个set >,但您的需求总是有可能在将来发生变化,需要设置 >>或vector 或者其他一些变化。通过使用typedef,您只需要在一个地方修复它,它将影响程序中SetInt的每个实例。与手动更改源文件中的十几个条目相比,这更容易并且更不容易出错。 ,less>

Due to points 2 and 3 above, typedef can be very powerful when used inside a class or a library, since it can strengthen the separation between interface and implementation which is generally considered good C++ form.

由于上面的第2点和第3点,当在类或库中使用时,typedef可以非常强大,因为它可以加强接口和实现之间的分离,这通常被认为是良好的C ++形式。

Of course, most of these advantages only really show themselves if you're using the same type multiple times in the same program. If you only expect to use the set<int, less<int> > once, typedef is probably overkill.

当然,如果你在同一个程序中多次使用相同的类型,这些优点中的大多数只能真正显示出来。如果您只希望使用set >一次,则typedef可能过度。 ,less>

#2


5  

Typedef's are mostly notational convenience, but they do have a bunch of benefits:

Typedef主要是符号方便,但它们确实有很多好处:

  • Declaring function pointer types (e.g. for callbacks)--notoriously hard to get right when spelled out completely.
  • 声明函数指针类型(例如,用于回调) - 在完全拼写出来时很难完成。

  • Dependent type information is easier to describe (e.g. SetInt::iterator) and should you ever change your mind (suppose you went to a hash_set instead of a std::set), is effectively updated immediately due to the typedef
  • 依赖类型信息更容易描述(例如SetInt :: iterator),如果你改变主意(假设你去了一个hash_set而不是std :: set),由于typedef而立即有效更新

  • They describe intent better than built-in typenames do (e.g. typedef int ErrCode)
  • 它们比内置类型名更好地描述了intent(例如typedef int ErrCode)

I'm sure there are more I'm not thinking of....

我相信还有更多我没想到......

#3


4  

You have it exactly correct. Using a typedef makes it easier to use things like SetInt::iterator.

你有完全正确的。使用typedef可以更容易地使用SetInt :: iterator之类的东西。

#4


3  

The benefit is when you refer to the same type multiple times. For example, if you want to iterate over your set; compare:

当您多次引用相同类型时,好处是。例如,如果要迭代您的集合;比较:

for (set<int, less<int> >::iterator i = xs.begin(); i != xs.end(); ++i)

with:

for (IntSet::iterator i = xs.begin(); i != xs.end(); ++i)

The difference gets more pronounced if you have nested template parameters (e.g. a set of pair of vector of string).

如果您有嵌套模板参数(例如一组字符串向量),则差异会更明显。

#5


2  

You are creating an alias to a type to be used in a specific context. How about this example:

您正在为要在特定上下文中使用的类型创建别名。这个例子怎么样:

typedef int INT32

That's all well and good, and if you only ever compiled for environments where an int was a 32-bit quantity you would not even need it. However, perhaps, some day in the future, you port your code to a system where a native int is 64 bits. Now your code may have some bugs if it assumes a 32-bit size for int's everywhere they are used. With the typedef, you need only change the argument:

这一切都很好,如果你只编译了int是32位数量的环境,你甚至不需要它。但是,也许在将来的某一天,您将代码移植到本机int为64位的系统中。现在你的代码可能会有一些错误,如果它假定int的32位大小在任何地方使用它们。使用typedef,您只需要更改参数:

typedef /*some 32 bit type*/ INT32 

Note that this is only an example. The idea is abstracting the type away if it may change in the future or clears up your code.

请注意,这只是一个例子。如果将来可能发生变化或清除代码,那么这个想法就会抽象出类型。