📄 00548.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<title>5.8.1 等价类型</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<meta name="generator" content="Doc-O-Matic" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="STYLESHEET" href="default.css" type="text/css" />
<script type="text/javascript" src="scripts.js"></script>
</head>
<body class="Element700" onload="onBodyLoadEx('systemverilog31a.html', 'topic', '00548.html');" onmousedown="onBodyMouseDown();">
<!-- Begin Popups -->
<div class="Element801" id="popup00111">
<div class="Element800">
<div class="Element14">
链接</div>
<div class="Element11">
<div class="Element10">
<a href="00547.html" target="topic">5.8 类型兼容性</a></div>
</div>
</div>
</div>
<!-- End Popups -->
<!-- Begin Page Header -->
<div class="Element710" id="areafixed">
<div class="Element92">
<table width="100%" cellspacing="0" cellpadding="0">
<tr><td width="33%">
<div class="Element1">
<a href="#" onmousedown="showPopup(this, 'popup00111');"><img src="seealsolink.png" border="0" alt="" title=""></a> SystemVerilog 3.1a语言参考手册</div>
</td><td width="34%">
<div class="Element2">
</div>
</td><td width="33%">
<div class="Element90">
<a href="00547.html" target="topic"><img src="btn_prev_lightblue.gif" border="0" alt="Previous" title="Previous" onmouseover="switchImage(this, 'btn_prev_lightblue_hover.gif');" onmouseout="switchImage(this, 'btn_prev_lightblue.gif');"></a><a href="00547.html" target="topic"><img src="btn_up_lightblue.gif" border="0" alt="Up" title="Up" onmouseover="switchImage(this, 'btn_up_lightblue_hover.gif');" onmouseout="switchImage(this, 'btn_up_lightblue.gif');"></a><a href="00549.html" target="topic"><img src="btn_next_lightblue.gif" border="0" alt="Next" title="Next" onmouseover="switchImage(this, 'btn_next_lightblue_hover.gif');" onmouseout="switchImage(this, 'btn_next_lightblue.gif');"></a></div>
</td></tr></table><div class="Element5">
5.8.1 等价类型</div>
</div>
</div>
<!-- End Page Header -->
<!-- Begin Client Area -->
<div class="Element720" id="areascroll">
<div class="Element721">
<!-- Begin Page Content -->
<div class="Element58">
<a name="描述"></a><div class="Element11">
<div class="Element10">
<p class="Element10">
如果两个数据类型符合下列的归纳定义,那么它们是等价的。如果两个数据类型不符合下列的归纳定义,那么它们就是不等价的。</p>
<ol class="Element630">
<li value="1" class="Element600">在每一个范围内,任何内建类型都等价于它本身的每一个其它的具体值。</li>
<li value="2" class="Element600">一个简单的<span style="color: #0000FF">typedef</span>或者重命名了一个内建的或用户定义类型的类型参数覆盖在类型标识符作用范围内等价于那个内建的或用户定义的数据类型。</li>
</ol><div class="Element170">
<a href="#" onclick="CopyElementToClipboard('code00202');">Copy Code</a></div>
<div class="Element13"><div class="Element12" id="code00202"><pre class="Element12"><span style="color: #0000FF">typedef bit</span> node<span style="color: #800080">;</span> <span style="color: #008000">// 'bit'与'node'是等价类型</span>
<span style="color: #0000FF">typedef</span> type1 type2<span style="color: #800080">;</span> <span style="color: #008000">// 'type1'与'type2'是等价类型</span></pre></div></div>
<ol class="Element630">
<li value="3" class="Element600">一个匿名的枚举、结构体、或联合体类型在相同的声明语句中声明的变量间是等价的。</li>
</ol><div class="Element170">
<a href="#" onclick="CopyElementToClipboard('code00203');">Copy Code</a></div>
<div class="Element13"><div class="Element12" id="code00203"><pre class="Element12"><span style="color: #0000FF">struct</span> <span style="color: #800080">{</span><span style="color: #0000FF">int</span> A<span style="color: #800080">;</span> <span style="color: #0000FF">int</span> B<span style="color: #800080">;}</span> AB1<span style="color: #800080">,</span> AB2<span style="color: #800080">;</span> <span style="color: #008000">// AB1, AB2具有等价的类型</span>
<span style="color: #0000FF">struct</span> <span style="color: #800080">{</span><span style="color: #0000FF">int</span> A<span style="color: #800080">;</span> <span style="color: #0000FF">int</span> B<span style="color: #800080">;}</span> AB3<span style="color: #800080">;</span> <span style="color: #008000">// AB3与AB1在类型上不等价</span></pre></div></div>
<ol class="Element630">
<li value="4" class="Element600">针对于枚举、非压缩结构体、非压缩联合体或类的<span style="color: #800080">typedef</span>等价于其本身以及在类型标识符作用范围内使用这个类型声明的变量。</li>
</ol><div class="Element170">
<a href="#" onclick="CopyElementToClipboard('code00204');">Copy Code</a></div>
<div class="Element13"><div class="Element12" id="code00204"><pre class="Element12"><span style="color: #0000FF">typedef struct</span> <span style="color: #800080">{</span><span style="color: #0000FF">int</span> A<span style="color: #800080">;</span> <span style="color: #0000FF">int</span> B<span style="color: #800080">;}</span> AB_t<span style="color: #800080">;</span>
AB_t AB1<span style="color: #800080">;</span>
AB_t AB2<span style="color: #800080">;</span> <span style="color: #008000">// AB1与AB2具有等价的类型</span>
<span style="color: #0000FF">typedef struct</span> <span style="color: #800080">{</span><span style="color: #0000FF">int</span> A<span style="color: #800080">;</span> <span style="color: #0000FF">int</span> B<span style="color: #800080">;}</span> otherAB_t<span style="color: #800080">;</span>
otherAB_t AB3<span style="color: #800080">;</span> <span style="color: #008000">// AB3与AB1或AB2在类型上不等价</span></pre></div></div>
<ol class="Element630">
<li value="5" class="Element600">如果压缩数组、压缩结构体、以及内建的integral类型的总位数相同,只要成员都是两态或四态,或者都是有符号或无符号的,那么它们是等价的。注意:如果压缩结构体或联合体的任意一位是四态的,那么整个结构体或联合体就被认为是四态的。</li>
</ol><div class="Element170">
<a href="#" onclick="CopyElementToClipboard('code00205');">Copy Code</a></div>
<div class="Element13"><div class="Element12" id="code00205"><pre class="Element12"><span style="color: #0000FF">typedef bit signed</span> <span style="color: #800080">[</span><span style="color: #FF0000">7</span><span style="color: #800080">:</span><span style="color: #FF0000">0</span><span style="color: #800080">]</span> BYTE<span style="color: #800080">;</span> <span style="color: #008000">// 等价于byte类型</span>
<span style="color: #0000FF">typedef struct packed signed</span> <span style="color: #800080">{</span><span style="color: #0000FF">bit</span><span style="color: #800080">[</span><span style="color: #FF0000">3</span><span style="color: #800080">:</span><span style="color: #FF0000">0</span><span style="color: #800080">]</span> a<span style="color: #800080">,</span> b<span style="color: #800080">;}</span> uint8<span style="color: #800080">;</span>
<span style="color: #008000"> // 等价于byte类型</span></pre></div></div>
<ol class="Element630">
<li value="6" class="Element600">如果非压缩数组类型具有等价的元素类型并且具有相同的形状,那么它们是等价的。形状指的是数组的维数以及每一维中元素的数目,并不是真实的维数范围。</li>
</ol><div class="Element170">
<a href="#" onclick="CopyElementToClipboard('code00206');">Copy Code</a></div>
<div class="Element13"><div class="Element12" id="code00206"><pre class="Element12"><span style="color: #0000FF">bit</span> <span style="color: #800080">[</span><span style="color: #FF0000">9</span><span style="color: #800080">:</span><span style="color: #FF0000">0</span><span style="color: #800080">]</span> A<span style="color: #800080">[</span><span style="color: #FF0000">0</span><span style="color: #800080">:</span><span style="color: #FF0000">5</span><span style="color: #800080">];</span>
<span style="color: #0000FF">bit</span> <span style="color: #800080">[</span><span style="color: #FF0000">1</span><span style="color: #800080">:</span><span style="color: #FF0000">10</span><span style="color: #800080">]</span> B<span style="color: #800080">[</span><span style="color: #FF0000">6</span><span style="color: #800080">];</span>
<span style="color: #0000FF">typedef bit</span> <span style="color: #800080">[</span><span style="color: #FF0000">10</span><span style="color: #800080">:</span><span style="color: #FF0000">1</span><span style="color: #800080">]</span> uint10<span style="color: #800080">;</span>
uint10 C<span style="color: #800080">[</span><span style="color: #FF0000">6</span><span style="color: #800080">:</span><span style="color: #FF0000">1</span><span style="color: #800080">];</span> <span style="color: #008000">// A, B以及C具有等价的类型</span>
<span style="color: #0000FF">typedef int</span> anint<span style="color: #800080">[</span><span style="color: #FF0000">0</span><span style="color: #800080">:</span><span style="color: #FF0000">0</span><span style="color: #800080">];</span> <span style="color: #008000">// anint在类型上与int不等价</span></pre></div></div>
<ol class="Element630">
<li value="7" class="Element600">显式地为一个类型加入<span style="color: #0000FF">signed</span>或<span style="color: #0000FF">unsigned</span>修饰符不会改变类型的缺省符号,也不会产生一个不等价的类型。然而,为了能够等价,符号必须匹配。</li>
</ol><div class="Element170">
<a href="#" onclick="CopyElementToClipboard('code00207');">Copy Code</a></div>
<div class="Element13"><div class="Element12" id="code00207"><pre class="Element12"><span style="color: #0000FF">typedef bit unsigned</span> ubit<span style="color: #800080">;</span> <span style="color: #008000">// 等价于bit的类型</span></pre></div></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -