static_cast (foo)vs.(int)foo

时间:2022-09-06 10:19:26

Could somebody please elaborate on the differences?

有人可以详细说明这些差异吗?

3 个解决方案

#1


The difference is that (int)foo can mean half a dozen different things. It might be a static_cast (convert between statically known types), it might be a const_cast (adding or removing const-ness), or it might be a reinterpret_cast (converting between pointer types)

区别在于(int)foo可能意味着六种不同的东西。它可能是static_cast(在静态已知类型之间转换),它可能是const_cast(添加或删除const),或者它可能是reinterpret_cast(在指针类型之间转换)

The compiler tries each of them until it finds one that works. Which means that it may not always pick the one you expect, so it can become a subtle source of bugs.

编译器会尝试每一个,直到找到一个有效的方法。这意味着它可能并不总是选择您期望的那个,因此它可能成为错误的微妙来源。

Further, static_cast is a lot easier to search for or do search/replace on.

此外,static_cast更容易搜索或搜索/替换。

#2


Look at what Stroustrup has to say about that, including the following:

看看Stroustrup对此有何看法,包括以下内容:

Because the C-style cast (T) can be used to express many logically different operations, the compiler has only the barest chance to catch misuses. [...]

因为C风格的强制转换(T)可以用来表达许多逻辑上不同的操作,所以编译器只有最小的机会来捕获误用。 [...]

The "new-style casts" were introduced to give programmers a chance to state their intentions more clearly and for the compiler to catch more errors. [...]

引入了“新式演员阵容”,让程序员有机会更清楚地表达他们的意图,并让编译器捕获更多错误。 [...]

In particular, C++ makes the distinction between static_cast and reinterpret_cast:

特别是,C ++区分了static_cast和reinterpret_cast:

The idea is that conversions allowed by static_cast are somewhat less likely to lead to errors than those that require reinterpret_cast. In principle, it is possible to use the result of a static_cast without casting it back to its original type, whereas you should always cast the result of a reinterpret_cast back to its original type before using it to ensure portability.

我们的想法是,static_cast允许的转换比那些需要reinterpret_cast的转换更不可能导致错误。原则上,可以使用static_cast的结果而不将其强制转换回原始类型,而在使用它之前,应始终将reinterpret_cast的结果转换回其原始类型以确保可移植性。

#3


(int) foo compares most to c++ reinterpret_cast<int>, i.e. no checks on the validity of the cast.

(int)foo与c ++ reinterpret_cast 进行比较,即没有检查强制转换的有效性。

#1


The difference is that (int)foo can mean half a dozen different things. It might be a static_cast (convert between statically known types), it might be a const_cast (adding or removing const-ness), or it might be a reinterpret_cast (converting between pointer types)

区别在于(int)foo可能意味着六种不同的东西。它可能是static_cast(在静态已知类型之间转换),它可能是const_cast(添加或删除const),或者它可能是reinterpret_cast(在指针类型之间转换)

The compiler tries each of them until it finds one that works. Which means that it may not always pick the one you expect, so it can become a subtle source of bugs.

编译器会尝试每一个,直到找到一个有效的方法。这意味着它可能并不总是选择您期望的那个,因此它可能成为错误的微妙来源。

Further, static_cast is a lot easier to search for or do search/replace on.

此外,static_cast更容易搜索或搜索/替换。

#2


Look at what Stroustrup has to say about that, including the following:

看看Stroustrup对此有何看法,包括以下内容:

Because the C-style cast (T) can be used to express many logically different operations, the compiler has only the barest chance to catch misuses. [...]

因为C风格的强制转换(T)可以用来表达许多逻辑上不同的操作,所以编译器只有最小的机会来捕获误用。 [...]

The "new-style casts" were introduced to give programmers a chance to state their intentions more clearly and for the compiler to catch more errors. [...]

引入了“新式演员阵容”,让程序员有机会更清楚地表达他们的意图,并让编译器捕获更多错误。 [...]

In particular, C++ makes the distinction between static_cast and reinterpret_cast:

特别是,C ++区分了static_cast和reinterpret_cast:

The idea is that conversions allowed by static_cast are somewhat less likely to lead to errors than those that require reinterpret_cast. In principle, it is possible to use the result of a static_cast without casting it back to its original type, whereas you should always cast the result of a reinterpret_cast back to its original type before using it to ensure portability.

我们的想法是,static_cast允许的转换比那些需要reinterpret_cast的转换更不可能导致错误。原则上,可以使用static_cast的结果而不将其强制转换回原始类型,而在使用它之前,应始终将reinterpret_cast的结果转换回其原始类型以确保可移植性。

#3


(int) foo compares most to c++ reinterpret_cast<int>, i.e. no checks on the validity of the cast.

(int)foo与c ++ reinterpret_cast 进行比较,即没有检查强制转换的有效性。