“类型安全”是什么意思?
当前回答
类型安全
在现代c++中,类型安全是非常重要的。类型安全意味着正确使用类型,因此避免不安全的类型强制转换和合并。c++中的每个对象都是根据其类型使用的,并且在使用之前需要对对象进行初始化。
安全初始化:{}
编译器在类型转换过程中防止信息丢失。例如, int {7};初始化没问题 \ . int b{7.5}编译器显示错误,因为信息丢失
不安全的初始化:=或()
编译器不会在类型转换期间防止信息丢失。 int a = 7初始化正常 int a = 7.5初始化正常,但信息丢失。a的实际值将变为7.0 int c(7)初始化OK int c(7.5)初始化是可以的,但是会发生信息丢失。a的实际值将变为7.0
其他回答
这里的许多答案将类型安全与静态类型和动态类型混为一谈。动态类型语言(如smalltalk)也可以是类型安全的。
简单的回答是:如果没有操作导致未定义的行为,则该语言被认为是类型安全的。许多人认为显式类型转换的要求对于严格类型的语言是必要的,因为自动转换有时会导致定义良好但意想不到/不直观的行为。
类型安全的代码只访问它被授权访问的内存位置,并且只能以定义良好的、允许的方式访问。 类型安全代码不能在对象上执行对该对象无效的操作。c#和VB。NET语言编译器总是生成类型安全的代码,这些代码在JIT编译期间被验证为类型安全的。
类型安全不应与静态/动态类型或强/弱类型相混淆。
类型安全语言是这样一种语言,在这种语言中,只能对数据执行数据类型所允许的操作。也就是说,如果您的数据是X类型,而X不支持操作y,那么该语言将不允许您执行y(X)。
这个定义没有设置何时检查的规则。它可以在编译时(静态类型)或在运行时(动态类型),通常通过异常进行。它可以两者兼有:一些静态类型语言允许您将数据从一种类型转换为另一种类型,并且必须在运行时检查转换的有效性(假设您试图将对象转换为消费者—编译器无法知道它是否可接受)。
Type-safety does not necessarily mean strongly typed, either - some languages are notoriously weakly typed, but still arguably type safe. Take Javascript, for example: its type system is as weak as they come, but still strictly defined. It allows automatic casting of data (say, strings to ints), but within well defined rules. There is to my knowledge no case where a Javascript program will behave in an undefined fashion, and if you're clever enough (I'm not), you should be able to predict what will happen when reading Javascript code.
类型不安全编程语言的一个例子是C语言:在数组边界之外读取/写入数组值的规范没有定义行为。预测将会发生什么是不可能的。C是一种具有类型系统的语言,但不是类型安全的。
“类型安全”的编程语言意味着以下几点:
不能从未初始化的变量中读取 数组的索引不能超出它们的边界 不能执行未检查的类型强制转换
类型安全意味着可以分配给程序变量的值集必须符合定义良好且可测试的标准。类型安全变量导致程序更加健壮,因为操作变量的算法可以相信变量只接受定义良好的一组值中的一个。保持这种信任可以确保数据和程序的完整性和质量。
For many variables, the set of values that may be assigned to a variable is defined at the time the program is written. For example, a variable called "colour" may be allowed to take on the values "red", "green", or "blue" and never any other values. For other variables those criteria may change at run-time. For example, a variable called "colour" may only be allowed to take on values in the "name" column of a "Colours" table in a relational database, where "red, "green", and "blue", are three values for "name" in the "Colours" table, but some other part of the computer program may be able to add to that list while the program is running, and the variable can take on the new values after they are added to the Colours table.
Many type-safe languages give the illusion of "type-safety" by insisting on strictly defining types for variables and only allowing a variable to be assigned values of the same "type". There are a couple of problems with this approach. For example, a program may have a variable "yearOfBirth" which is the year a person was born, and it is tempting to type-cast it as a short integer. However, it is not a short integer. This year, it is a number that is less than 2009 and greater than -10000. However, this set grows by 1 every year as the program runs. Making this a "short int" is not adequate. What is needed to make this variable type-safe is a run-time validation function that ensures that the number is always greater than -10000 and less than the next calendar year. There is no compiler that can enforce such criteria because these criteria are always unique characteristics of the problem domain.
Languages that use dynamic typing (or duck-typing, or manifest typing) such as Perl, Python, Ruby, SQLite, and Lua don't have the notion of typed variables. This forces the programmer to write a run-time validation routine for every variable to ensure that it is correct, or endure the consequences of unexplained run-time exceptions. In my experience, programmers in statically typed languages such as C, C++, Java, and C# are often lulled into thinking that statically defined types is all they need to do to get the benefits of type-safety. This is simply not true for many useful computer programs, and it is hard to predict if it is true for any particular computer program.
长和短....您需要类型安全吗?如果是,那么编写运行时函数来确保当变量被赋值时,它符合定义良好的标准。缺点是它使域分析对于大多数计算机程序来说非常困难,因为您必须显式地为每个程序变量定义标准。