类型双关

类型双关是计算机科学的术语，指任何编程技术能颠覆或者绕过一门程序设计语言的类型系统，以达成在形式语言内部难以甚至不可能实现的效果。
C语言与C++语言，语法结构如类型转换与codice_1，以及C++增加的codice_2运算符，用于实现类型双关。
Pascal语言使用records与variants来按照多种方法处理特定数据类型。
Socket例子.
Berkeley sockets使用类型双关来处理IP地址。函数bind绑定一个位初始化的套接字到一个IP地址，其声明如下：
<syntaxhighlight lang="c">
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
</syntaxhighlight>
codice_3函数通常如此使用:
<syntaxhighlight lang="c">
struct sockaddr_in sa = {0};
int sockfd = ...;
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
bind(sockfd, (struct sockaddr *)&sa, sizeof sa);
</syntaxhighlight>
这是因为codice_4与codice_5有相同的内存布局。两个类型的指针可以互相转换。 
浮点例子.
类型双关不仅限于struct。对于浮点数，判断其是否为负值：
<syntaxhighlight lang="c">
bool is_negative(float x) {
}</syntaxhighlight>
假定浮点比较的代价高昂，并假定浮点数用IEEE 754标准，就可以用类型双关获取浮点数的符号位（sign bit）做整型比较： 
<syntaxhighlight lang="c">
bool is_negative(float x) {
}</syntaxhighlight>
注意有一些特例，如codice_6是负0，前一种实现返回codice_7而第二种实现返回codice_8.
这样的实现适合于实时计算而又不能被优化实现的情形。注意把所有假定均写为注释记录下来，并写入静态断言（static assertions）验证可移植期望是否满足。雷神之锤III竞技场游戏用此方法实现平方根倒数速算法。
使用codice_1.
为了遵循C99/C++的严格别名规则，可以使用codice_1:
<syntaxhighlight lang="c">
bool is_negative(float x) {
}</syntaxhighlight>
GCC编译器支持这样的语言扩展。
其他的类型双关，见数组步长。
Pascal.
A variant record permits treating a data type as multiple kinds of data depending on which variant is being referenced. In the following example, "integer" is presumed to be 16 bit, while "longint" and "real" are presumed to be 32, while character is presumed to be 8 bit:
<syntaxhighlight lang="pascal">
</syntaxhighlight>
In Pascal, copying a real to an integer converts it to the truncated value. This method would translate the binary value of the floating-point number into whatever it is as a long integer (32 bit), which will not be the same and may be incompatible with the long integer value on some systems.
These examples could be used to create strange conversions, although, in some cases, there may be legitimate uses for these types of constructs, such as for determining locations of particular pieces of data. In the following example a pointer and a longint are both presumed to be 32 bit:
<syntaxhighlight lang="pascal">
</syntaxhighlight>
Where "new" is the standard routine in Pascal for allocating memory for a pointer, and "hex" is presumably a routine to print the hexadecimal string describing the value of an integer. This would allow the display of the address of a pointer, something which is not normally permitted. (Pointers cannot be read or written, only assigned .) Assigning a value to an integer variant of a pointer would allow examining or writing to any location in system memory:
<syntaxhighlight lang="pascal">
</syntaxhighlight>
This construct may cause a program check or protection violation if address 0 is protected against reading on the machine the program is running upon or the operating system it is running under.
C#.
In C# (and other .NET languages), this is a bit harder to achieve because of the type system, but can be done nonetheless, using pointers or struct unions.
Pointers.
C# only allows pointers to so-called native types, i.e. any primitive type (except codice_11), enum, array or struct that is composed only of other native types. Note that pointers are only allowed in code blocks marked 'unsafe'.
<syntaxhighlight lang="csharp">
</syntaxhighlight>
Struct unions.
Struct unions are allowed without any notion of 'unsafe' code, but they do require the definition of a new type.
<syntaxhighlight lang="csharp">
</syntaxhighlight>
Raw CIL code.
Raw CIL can be used instead of C#, because it doesn't have most of the type limitations. This allows one to, for example, combine two enum values of a generic type:
<syntaxhighlight lang="csharp">
</syntaxhighlight>
This can be circumvented by the following CIL code:
<syntaxhighlight lang="csharp">
</syntaxhighlight>
The codice_12 CIL opcode allows for some other tricks, such as converting a struct to a byte array:
<syntaxhighlight lang="csharp">
</syntaxhighlight>