是否认为C ++是弱类型?为什么? [英] Is C++ considered weakly typed? Why?

查看:130
本文介绍了是否认为C ++是弱类型?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为C ++是其中最强大的类型化语言之一。

所以我很震惊,看到本文表3 指出C ++是弱类型的。

I've always considered C++ to be one of the most strongly typed languages out there.
So I was quite shocked to see Table 3 of this paper state that C++ is weakly typed.

显然,


C和C ++被认为是弱类型的,因为由于类型转换,一个人可以解释一个结构的字段。

C and C++ are considered weakly typed since, due to type-casting, one can interpret a field of a structure that was an integer as a pointer.

是否存在类型转换是否很重要?这样的强制转换的显式性没有关系吗?

Is the existence of type casting all that matters? Does the explicit-ness of such casts not matter?

更笼统地说,是否真的普遍接受C ++是弱类型?为什么?

More generally, is it really generally accepted that C++ is weakly typed? Why?

推荐答案

该论文首先声称:


相反,如果类型混淆可以静默地发生(未检测到),并且最终会导致难以定位的错误,那么该语言的类型就很弱。

In contrast, a language is weakly-typed if type-confusion can occur silently (undetected), and eventually cause errors that are difficult to localize.

然后声明:


此外,由于类型转换,C和C ++被认为是弱类型的,可以将一个整数结构的字段解释为指针。

Also, C and C++ are considered weakly typed since, due to type-casting, one can interpret a field of a structure that was an integer as a pointer.

在我看来,这是一个矛盾。在C和C ++中,由于强制类型转换而引起的类型混淆不会默默地发生-有强制转换!这并不表明这些语言中的任何一种都是弱类型的,至少不是根据该文件中的定义。

This seems like a contradiction to me. In C and C++, the type-confusion that can occur as a result of casts will not occur silently -- there's a cast! This does not demonstrate that either of those languages is weakly-typed, at least not by the definition in that paper.

也就是说,根据该文件中的定义,C和C ++ 仍被认为是弱类型的。正如已经在该问题的评论中所指出的那样,该语言支持隐式类型转换的情况。许多类型可以隐式转换为 bool ,类型为 int 的原义零可以静默转换为任何指针类型,在本文中,考虑到大小不同的整数之间的转换等,因此这似乎是考虑使用C和C ++弱类型的好理由。

That said, by the definition in the paper, C and C++ may still be considered weakly-typed. There are, as noted in the comments on the question already, cases where the language supports implicit type conversions. Many types can be implicitly converted to bool, a literal zero of type int can be silently converted to any pointer type, there are conversions between integers of varying sizes, etc, so this seems like a good reason to consider C and C++ weakly-typed for the purposes of the paper.

对于C (但不是C ++),还有更危险的隐式转换值得一提:

For C (but not C++), there are also more dangerous implicit conversions that are worth mentioning:

int main() {
  int i = 0;
  void *v = &i;
  char *c = v;
  return *c;
}

就本文而言,绝对必须将其视为弱类型。重新解释位是无声的,通过将其修改为使用完全不相关的类型可以使情况更糟,该类型具有未定义的沉默行为,通常具有与重新解释位相同的效果,但是在启用优化后会以神秘但有时有趣的方式爆炸

For the purposes of the paper, that must definitely be considered weakly-typed. The reinterpretation of bits happens silently, and can be made far worse by modifying it to use completely unrelated types, which has silent undefined behaviour that typically has the same effect as reinterpreting bits, but blows up in mysterious yet sometimes amusing ways when optimisations are enabled.

不过,总体而言,我认为强类型和弱类型没有固定的定义。有各种等级,与汇编语言相比,强类型的语言与汇编语言相比可能弱类型。要确定C或C ++是弱类型的,首先必须询问您要弱类型的含义。

In general, though, I think there isn't a fixed definition of "strongly-typed" and "weakly-typed". There are various grades, a language that is strongly-typed compared to assembly may be weakly-typed compared to Pascal. To determine whether C or C++ is weakly-typed, you first have to ask what you want weakly-typed to mean.

这篇关于是否认为C ++是弱类型?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆