"Fossies" - the Fresh Open Source Software Archive

Member "dmd2/html/d/spec/enum.html" (20 Nov 2020, 30301 Bytes) of package /linux/misc/dmd.2.094.2.linux.tar.xz:


The requested HTML page contains a <FORM> tag that is unusable on "Fossies" in "automatic" (rendered) mode so that page is shown as HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 <!DOCTYPE html>
    3 <html lang="en-US">
    4 <!--
    5     Copyright (c) 1999-2020 by the D Language Foundation
    6     All Rights Reserved.
    7     https://dlang.org/foundation_overview.html
    8   -->
    9 <head>
   10 <meta charset="utf-8">
   11 <meta name="keywords" content="D programming language">
   12 <meta name="description" content="D Programming Language">
   13 <title>Enums - D Programming Language</title>
   14 
   15 <link rel="stylesheet" href="../css/codemirror.css">
   16 <link rel="stylesheet" href="../css/style.css">
   17 <link rel="stylesheet" href="../css/print.css" media="print">
   18 <link rel="shortcut icon" href="../favicon.ico">
   19 <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0">
   20 
   21 </head>
   22 <body id='Enums' class='doc'>
   23 <script type="text/javascript">document.body.className += ' have-javascript'</script>
   24 <div id="top"><div class="helper"><div class="helper expand-container">    <div class="logo"><a href=".."><img id="logo" alt="D Logo" src="../images/dlogo.svg"></a></div>
   25     <a href="../menu.html" title="Menu" class="hamburger expand-toggle"><span>Menu</span></a>
   26     
   27 <div id="cssmenu"><ul>    <li><a href='https://tour.dlang.org'><span>Learn</span></a></li>
   28     <li class='expand-container'><a class='expand-toggle' href='../documentation.html'><span>Documentation</span></a>
   29       
   30 <ul class='expand-content'>    <li><a href='../spec/spec.html'>Language Reference</a></li>
   31     <li><a href='../phobos/index.html'>Library Reference</a></li>
   32     <li><a href='../dmd.html'>Command-line Reference</a></li>
   33     <li class="menu-divider"><a href='../comparison.html'>Feature Overview</a></li>
   34     <li><a href='../articles.html'>Articles</a></li>
   35  </ul></li>
   36     <li><a href='../download.html'><span>Downloads</span></a></li>
   37     <li><a href='https://code.dlang.org'><span>Packages</span></a></li>
   38     <li class='expand-container'><a class='expand-toggle' href='../community.html'><span>Community</span></a>
   39       
   40 <ul class='expand-content'>    <li><a href='https://dlang.org/blog'>Blog</a></li>
   41     <li><a href='../orgs-using-d.html'>Orgs using D</a></li>
   42     <li><a href='https://twitter.com/search?q=%23dlang'>Twitter</a></li>
   43     <li><a href='../calendar.html'>Calendar</a></li>
   44     <li class="menu-divider"><a href='https://forum.dlang.org'>Forums</a></li>
   45     <li><a href='irc://irc.freenode.net/d'>IRC</a></li>
   46     <li><a href='https://discord.gg/bMZk9Q4'>Community Discord</a></li>
   47     <li><a href='https://wiki.dlang.org'>Wiki</a></li>
   48     <li class="menu-divider"><a href='https://github.com/dlang'>GitHub</a></li>
   49     <li><a href='../bugstats.html'>Issues</a></li>
   50     <li><a href='https://wiki.dlang.org/Get_involved'>Get involved</a></li>
   51     <li class="menu-divider"><a href='../foundation/contributors.html'>Contributors</a></li>
   52     <li><a href='../foundation/index.html'>Foundation</a></li>
   53     <li><a href='..//security.html'>Security Team</a></li>
   54     <li><a href='../foundation/donate.html'>Donate</a></li>
   55     <li><a href='../foundation/sponsors.html'>Sponsors</a></li>
   56  </ul></li>
   57     <li class='expand-container'><a class='expand-toggle' href='../resources.html'><span>Resources</span></a>
   58       
   59 <ul class='expand-content'>    <li><a href='https://tour.dlang.org'>Tour</a></li>
   60     <li><a href='https://wiki.dlang.org/Books'>Books</a></li>
   61     <li><a href='https://wiki.dlang.org/Tutorials'>Tutorials</a></li>
   62     <li class="menu-divider"><a href='https://wiki.dlang.org/Development_tools'>Tools</a></li>
   63     <li><a href='https://wiki.dlang.org/Editors'>Editors</a></li>
   64     <li><a href='https://wiki.dlang.org/IDEs'>IDEs</a></li>
   65     <li><a href='https://run.dlang.io'>run.dlang.io</a></li>
   66     <li><a href='http://rainers.github.io/visuald/visuald/StartPage.html'>Visual D</a></li>
   67     <li class="menu-divider"><a href='../acknowledgements.html'>Acknowledgments</a></li>
   68     <li><a href='../dstyle.html'>D Style</a></li>
   69     <li><a href='../glossary.html'>Glossary</a></li>
   70     <li><a href='../sitemap.html'>Sitemap</a></li>
   71  </ul></li>
   72 </ul></div>
   73     <div class="search-container expand-container">        <a href="../search.html" class="expand-toggle" title="Search"><span>Search</span></a>
   74         
   75     <div id="search-box">        <form method="get" action="https://google.com/search">
   76             <input type="hidden" id="domains" name="domains" value="dlang.org">
   77             <input type="hidden" id="sourceid" name="sourceid" value="google-search">
   78             <span id="search-query"><input id="q" name="q" placeholder="Search"></span><span id="search-dropdown"><span class="helper">                <select id="sitesearch" name="sitesearch" size="1">
   79                     <option value="dlang.org">Entire Site</option>
   80                     <option selected value="dlang.org/spec">Language</option>
   81                     <option  value="dlang.org/phobos">Library</option>
   82                     <option  value="forum.dlang.org">Forums</option>
   83                     
   84                 </select>
   85             </span></span><span id="search-submit"><button type="submit"><i class="fa fa-search"></i><span>go</span></button></span>
   86         </form>
   87     </div>
   88     </div>
   89 </div></div></div>
   90 
   91 <div class="container">    
   92 <div class="subnav-helper"></div> <div class="subnav">    <div class="head"><h5>Language Reference</h5> <p class="subnav-duplicate"><a href="../spec/spec.html">table of contents</a></p></div>
   93     <ul>        <li><a href='            ../spec/intro.html'>Introduction</a></li><li><a href='            ../spec/lex.html'>Lexical</a></li><li><a href='            ../spec/grammar.html'>Grammar</a></li><li><a href='            ../spec/module.html'>Modules</a></li><li><a href='            ../spec/declaration.html'>Declarations</a></li><li><a href='            ../spec/type.html'>Types</a></li><li><a href='            ../spec/property.html'>Properties</a></li><li><a href='            ../spec/attribute.html'>Attributes</a></li><li><a href='            ../spec/pragma.html'>Pragmas</a></li><li><a href='            ../spec/expression.html'>Expressions</a></li><li><a href='            ../spec/statement.html'>Statements</a></li><li><a href='            ../spec/arrays.html'>Arrays</a></li><li><a href='            ../spec/hash-map.html'>Associative Arrays</a></li><li><a href='            ../spec/struct.html'>Structs and Unions</a></li><li><a href='            ../spec/class.html'>Classes</a></li><li><a href='            ../spec/interface.html'>Interfaces</a></li><li><a href='            ../spec/enum.html'>Enums</a></li><li><a href='            ../spec/const3.html'>Type Qualifiers</a></li><li><a href='            ../spec/function.html'>Functions</a></li><li><a href='            ../spec/operatoroverloading.html'>Operator Overloading</a></li><li><a href='            ../spec/template.html'>Templates</a></li><li><a href='            ../spec/template-mixin.html'>Template Mixins</a></li><li><a href='            ../spec/contracts.html'>Contract Programming</a></li><li><a href='            ../spec/version.html'>Conditional Compilation</a></li><li><a href='            ../spec/traits.html'>Traits</a></li><li><a href='            ../spec/errors.html'>Error Handling</a></li><li><a href='            ../spec/unittest.html'>Unit Tests</a></li><li><a href='            ../spec/garbage.html'>Garbage Collection</a></li><li><a href='            ../spec/float.html'>Floating Point</a></li><li><a href='            ../spec/iasm.html'>D x86 Inline Assembler</a></li><li><a href='            ../spec/ddoc.html'>Embedded Documentation</a></li><li><a href='            ../spec/interfaceToC.html'>Interfacing to C</a></li><li><a href='            ../spec/cpp_interface.html'>Interfacing to C++</a></li><li><a href='            ../spec/objc_interface.html'>Interfacing to Objective-C</a></li><li><a href='            ../spec/portability.html'>Portability Guide</a></li><li><a href='            ../spec/entity.html'>Named Character Entities</a></li><li><a href='            ../spec/memory-safe-d.html'>Memory Safety</a></li><li><a href='            ../spec/abi.html'>Application Binary Interface</a></li><li><a href='            ../spec/simd.html'>Vector Extensions</a></li><li><a href='            ../spec/betterc.html'>Better C</a></li><li><a href='            ../spec/ob.html'>Live Functions
   94         </a></li>
   95     </ul>
   96 </div>
   97     <div class="hyphenate" id="content">        
   98 <div id="tools"><div >  <div class="tip smallprint">        <a href="https://issues.dlang.org/enter_bug.cgi?bug_file_loc=http%3A%2F%2Fdlang.org/&amp;component=dlang.org&amp;op_sys=All&amp;priority=P3&amp;product=D&amp;rep_platform=All&amp;short_desc=%5BEnums%5D&amp;version=D2&amp;bug_severity=enhancement">Report a bug</a>
   99         <div >          If you spot a problem with this page, click here to create a Bugzilla issue.
  100         </div>
  101     </div>
  102     <div class="tip smallprint">        <a href="https://github.com/dlang/dlang.org/edit/master/spec/enum.dd">Improve this page</a>
  103         <div >          Quickly fork, edit online, and submit a pull request for this page.
  104             Requires a signed-in GitHub account. This works well for small changes.
  105             If you'd like to make larger changes you may want to consider using
  106             a local clone.
  107         </div>
  108     </div>
  109 </div></div>
  110         <h1>Enums</h1>
  111         
  112 <style>    body { counter-reset: h1 17; counter-increment: h1 -1; }
  113     h1 { counter-reset: h2 h3 p; }
  114     h2 { counter-reset: h3 h4 p; }
  115     h3 { counter-reset: h4 p; }
  116     h4 { counter-reset: p; }
  117     h1::before {
  118         counter-increment: h1;
  119         content: counter(h1) ". ";
  120     }
  121     h2::before {
  122         counter-increment: h2;
  123         content: counter(h1) "." counter(h2) " ";
  124     }
  125     h3::before {
  126         counter-increment: h3;
  127         content: counter(h1) "." counter(h2) "." counter(h3) " ";
  128     }
  129     h4::before {
  130         counter-increment: h4;
  131         content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " ";
  132     }
  133     p::before, .spec-boxes::before {
  134         counter-increment: p;
  135         content: counter(p) ". ";
  136     }
  137     h1::before, h2::before, h3::before, h4::before, p::before, .spec-boxes::before
  138     {
  139         color: #999;
  140         font-size: 80%;
  141         margin-right: 0.25em;
  142     }
  143 </style>
  144         <div class="blankline"></div>
  145 <div class="blankline"></div>
  146 <div class="blankline"></div>
  147 <div class="page-contents quickindex">    <div class="page-contents-header">        <b>Contents</b>
  148     </div>
  149     <ol>    <li><a href="#named_enums">Named Enums</a><ol>        <li><a href="#enum_default_initializer">Enum Default Initializer</a></li>
  150         <li><a href="#enum_properties">Enum Properties</a></li>
  151     </ol></li>
  152     <li><a href="#anonymous_enums">Anonymous Enums</a></li>
  153     <li><a href="#manifest_constants">Manifest Constants</a></li>
  154 </ol>
  155 </div>
  156 <div class="blankline"></div>
  157 <pre class="bnf notranslate"><a id="EnumDeclaration"><span class="gname">EnumDeclaration</span></a>:
  158     <span class="d_inlinecode donthyphenate notranslate">enum</span> <a href="../spec/lex.html#Identifier"><i>Identifier</i></a> <a href="#EnumBody"><i>EnumBody</i></a>
  159     <span class="d_inlinecode donthyphenate notranslate">enum</span> <a href="../spec/lex.html#Identifier"><i>Identifier</i></a> <span class="d_inlinecode donthyphenate notranslate">:</span> <a href="#EnumBaseType"><i>EnumBaseType</i></a> <a href="#EnumBody"><i>EnumBody</i></a>
  160     <a href="#AnonymousEnumDeclaration"><i>AnonymousEnumDeclaration</i></a>
  161 <div class="blankline"></div>
  162 <a id="EnumBaseType"><span class="gname">EnumBaseType</span></a>:
  163     <a href="../spec/declaration.html#Type"><i>Type</i></a>
  164 <div class="blankline"></div>
  165 <a id="EnumBody"><span class="gname">EnumBody</span></a>:
  166     <span class="d_inlinecode donthyphenate notranslate">{</span> <a href="#EnumMembers"><i>EnumMembers</i></a> <span class="d_inlinecode donthyphenate notranslate">}</span>
  167     <span class="d_inlinecode donthyphenate notranslate">;</span>
  168 <div class="blankline"></div>
  169 <a id="EnumMembers"><span class="gname">EnumMembers</span></a>:
  170     <a href="#EnumMember"><i>EnumMember</i></a>
  171     <a href="#EnumMember"><i>EnumMember</i></a> <span class="d_inlinecode donthyphenate notranslate">,</span>
  172     <a href="#EnumMember"><i>EnumMember</i></a> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="#EnumMembers"><i>EnumMembers</i></a>
  173 <div class="blankline"></div>
  174 <a id="EnumMemberAttributes"><span class="gname">EnumMemberAttributes</span></a>:
  175     <a href="#EnumMemberAttribute"><i>EnumMemberAttribute</i></a>
  176     <a href="#EnumMemberAttribute"><i>EnumMemberAttribute</i></a> <a href="#EnumMemberAttributes"><i>EnumMemberAttributes</i></a>
  177 <div class="blankline"></div>
  178 <a id="EnumMemberAttribute"><span class="gname">EnumMemberAttribute</span></a>:
  179     <a href="../spec/attribute.html#DeprecatedAttribute"><i>DeprecatedAttribute</i></a>
  180     <a href="../spec/attribute.html#UserDefinedAttribute"><i>UserDefinedAttribute</i></a>
  181     <span class="d_inlinecode donthyphenate notranslate">@</span><a href="../spec/attribute.html#disable"><i>disable</i></a>
  182 <div class="blankline"></div>
  183 <a id="EnumMember"><span class="gname">EnumMember</span></a>:
  184     <a href="#EnumMemberAttributes"><i>EnumMemberAttributes</i></a><sub>opt</sub> <a href="../spec/lex.html#Identifier"><i>Identifier</i></a>
  185     <a href="#EnumMemberAttributes"><i>EnumMemberAttributes</i></a><sub>opt</sub> <a href="../spec/lex.html#Identifier"><i>Identifier</i></a> <span class="d_inlinecode donthyphenate notranslate">=</span> <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>
  186 <div class="blankline"></div>
  187 <a id="AnonymousEnumDeclaration"><span class="gname">AnonymousEnumDeclaration</span></a>:
  188     <span class="d_inlinecode donthyphenate notranslate">enum</span> <span class="d_inlinecode donthyphenate notranslate">:</span> <a href="#EnumBaseType"><i>EnumBaseType</i></a> <span class="d_inlinecode donthyphenate notranslate">{</span> <a href="#EnumMembers"><i>EnumMembers</i></a> <span class="d_inlinecode donthyphenate notranslate">}</span>
  189     <span class="d_inlinecode donthyphenate notranslate">enum</span> <span class="d_inlinecode donthyphenate notranslate">{</span> <a href="#EnumMembers"><i>EnumMembers</i></a> <span class="d_inlinecode donthyphenate notranslate">}</span>
  190     <span class="d_inlinecode donthyphenate notranslate">enum</span> <span class="d_inlinecode donthyphenate notranslate">{</span> <a href="#AnonymousEnumMembers"><i>AnonymousEnumMembers</i></a> <span class="d_inlinecode donthyphenate notranslate">}</span>
  191 <div class="blankline"></div>
  192 <a id="AnonymousEnumMembers"><span class="gname">AnonymousEnumMembers</span></a>:
  193     <a href="#AnonymousEnumMember"><i>AnonymousEnumMember</i></a>
  194     <a href="#AnonymousEnumMember"><i>AnonymousEnumMember</i></a> <span class="d_inlinecode donthyphenate notranslate">,</span>
  195     <a href="#AnonymousEnumMember"><i>AnonymousEnumMember</i></a> <span class="d_inlinecode donthyphenate notranslate">,</span> <a href="#AnonymousEnumMembers"><i>AnonymousEnumMembers</i></a>
  196 <div class="blankline"></div>
  197 <a id="AnonymousEnumMember"><span class="gname">AnonymousEnumMember</span></a>:
  198     <a href="#EnumMember"><i>EnumMember</i></a>
  199     <a href="../spec/declaration.html#Type"><i>Type</i></a> <a href="../spec/lex.html#Identifier"><i>Identifier</i></a> <span class="d_inlinecode donthyphenate notranslate">=</span> <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>
  200 </pre>
  201 <div class="blankline"></div>
  202         <p>Enum declarations are used to define a group of constants.
  203         </p>
  204 <div class="blankline"></div>
  205 <h2><a class="anchor" title="Permalink to this section" id="named_enums" href="#named_enums">Named Enums</a></h2>
  206 <div class="blankline"></div>
  207         <p>        Named enums are used to declare related
  208         constants and group them by giving them a unique type.
  209         The <a href="#EnumMembers"><i>EnumMembers</i></a> are declared in the scope of the named enum. The named
  210         enum declares a new type, and all the <i>EnumMembers</i> have that type.
  211         </p>
  212 <div class="blankline"></div>
  213         <p>This defines a new type <span class="d_inlinecode donthyphenate notranslate">X</span> which has values
  214         <span class="d_inlinecode donthyphenate notranslate">X.A=0</span>, <span class="d_inlinecode donthyphenate notranslate">X.B=1</span>, <span class="d_inlinecode donthyphenate notranslate">X.C=2</span>:</p>
  215 <div class="blankline"></div>
  216 <pre class="d_code notranslate"><span class="d_keyword">enum</span> X { A, B, C }  <span class="d_comment">// named enum
  217 </span></pre>
  218 <div class="blankline"></div>
  219 <div class="blankline"></div>
  220         <p>If the <i>EnumBaseType</i> is not explicitly set, and the first
  221         <i>EnumMember</i> has an <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>, it is set to the type of that
  222         <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>. Otherwise, it defaults to
  223         type <span class="d_inlinecode donthyphenate notranslate">int</span>.</p>
  224 <div class="blankline"></div>
  225         <p>Named enum members may not have individual <i>Type</i>s.
  226         </p>
  227 <div class="blankline"></div>
  228 <div class="blankline"></div>
  229         <p>A named enum member can be implicitly cast to its <a href="#EnumBaseType"><i>EnumBaseType</i></a>,
  230         but <i>EnumBaseType</i> types
  231         cannot be implicitly cast to an enum type.
  232         </p>
  233 <div class="blankline"></div>
  234         <p>The value of an <a href="#EnumMember"><i>EnumMember</i></a> is given by its <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>.
  235         If there is no <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a> and it is the first <i>EnumMember</i>,
  236         its value is <a href="#EnumBaseType"><i>EnumBaseType</i></a><span class="d_inlinecode donthyphenate notranslate">.init</span>.
  237         </p>
  238 <div class="blankline"></div>
  239         <p>If there is no <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a> and it is not the first <i>EnumMember</i>,
  240         it is given the value of the previous <i>EnumMember</i><span class="d_inlinecode donthyphenate notranslate">+1</span>.
  241         If the value of the previous <i>EnumMember</i> is <a href="#EnumBaseType"><i>EnumBaseType</i></a><span class="d_inlinecode donthyphenate notranslate">.max</span>,
  242         it is an error.
  243         If the value of the previous <i>EnumMember</i><span class="d_inlinecode donthyphenate notranslate">+1</span> is the same as the
  244         value of the previous <i>EnumMember</i>, it is an error. (This can happen
  245         with floating point types.)
  246         </p>
  247 <div class="blankline"></div>
  248         <p>All <i>EnumMember</i>s are in scope for the <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>s.
  249         </p>
  250 <div class="blankline"></div>
  251 <pre class="d_code notranslate"><span class="d_keyword">enum</span> A = 3;
  252 <span class="d_keyword">enum</span> B
  253 {
  254     A = A <span class="d_comment">// error, circular reference
  255 </span>}
  256 <span class="d_keyword">enum</span> C
  257 {
  258     A = B,  <span class="d_comment">// A = 4
  259 </span>    B = D,  <span class="d_comment">// B = 4
  260 </span>    C = 3,  <span class="d_comment">// C = 3
  261 </span>    D       <span class="d_comment">// D = 4
  262 </span>}
  263 <span class="d_keyword">enum</span> E : C
  264 {
  265     E1 = C.D,
  266     E2      <span class="d_comment">// error, C.D is C.max
  267 </span>}
  268 </pre>
  269 <div class="blankline"></div>
  270 <div class="blankline"></div>
  271         <p>An empty enum body (For example <span class="d_inlinecode donthyphenate notranslate">enum E;</span>) signifies an opaque
  272         enum - the enum members are unknown.</p>
  273 <div class="blankline"></div>
  274 <h3><a class="anchor" title="Permalink to this section" id="enum_default_initializer" href="#enum_default_initializer">Enum Default Initializer</a></h3>
  275 <div class="blankline"></div>
  276         <p>The <span class="d_inlinecode donthyphenate notranslate">.init</span> property of an enum type is the value
  277         of the first member of that enum.
  278         This is also the default initializer for the enum type.
  279         </p>
  280 <div class="blankline"></div>
  281 <pre class="d_code notranslate"><span class="d_keyword">enum</span> X { A=3, B, C }
  282 X x;      <span class="d_comment">// x is initialized to 3
  283 </span></pre>
  284 <div class="blankline"></div>
  285 <h3><a class="anchor" title="Permalink to this section" id="enum_properties" href="#enum_properties">Enum Properties</a></h3>
  286 <div class="blankline"></div>
  287         <p>Enum properties only exist for named enums.
  288         </p>
  289 <div class="blankline"></div>
  290         <table>        <caption>Named Enum Properties</caption>
  291         <tr><td><span class="d_inlinecode donthyphenate notranslate">.init</span></td><td>First enum member value</td></tr>
  292         <tr><td><span class="d_inlinecode donthyphenate notranslate">.min</span></td><td>Smallest enum member value</td></tr>
  293         <tr><td><span class="d_inlinecode donthyphenate notranslate">.max</span></td><td>Largest enum member value</td></tr>
  294         <tr><td><span class="d_inlinecode donthyphenate notranslate">.sizeof</span></td><td>Size of storage for an enumerated value</td></tr>
  295         </table>
  296 <div class="blankline"></div>
  297         <p>For example:</p>
  298 <div class="blankline"></div>
  299 <pre class="d_code notranslate"><span class="d_keyword">enum</span> X { A=3, B=1, C=4, D, E=2 }
  300 X.init   <span class="d_comment">// is X.A
  301 </span>X.min    <span class="d_comment">// is X.B
  302 </span>X.max    <span class="d_comment">// is X.D
  303 </span>X.sizeof <span class="d_comment">// is same as int.sizeof
  304 </span></pre>
  305 <div class="blankline"></div>
  306         <p>The <a href="#EnumBaseType"><i>EnumBaseType</i></a> of named enums must support comparison
  307         in order to compute the <span class="d_inlinecode donthyphenate notranslate">.max</span> and <span class="d_inlinecode donthyphenate notranslate">.min</span> properties.
  308         </p>
  309 <div class="blankline"></div>
  310 <div class="blankline"></div>
  311 <h2><a class="anchor" title="Permalink to this section" id="anonymous_enums" href="#anonymous_enums">Anonymous Enums</a></h2>
  312 <div class="blankline"></div>
  313         <p>If the enum <i>Identifier</i> is not present, then the enum
  314         is an <i>anonymous enum</i>, and the <a href="#EnumMembers"><i>EnumMembers</i></a> are declared
  315         in the scope the <a href="#EnumDeclaration"><i>EnumDeclaration</i></a> appears in.
  316         No new type is created.
  317         </p>
  318 <div class="blankline"></div>
  319         <p>The <i>EnumMembers</i> can have different types.
  320         Those types are given by the first of:
  321         </p>
  322 <div class="blankline"></div>
  323         <ol>        <li>The <i>Type</i>, if present. Types are not permitted when an
  324                 <a href="#EnumBaseType"><i>EnumBaseType</i></a> is present.</li>
  325         <li>The <i>EnumBaseType</i>, if present.</li>
  326         <li>The type of the <i>AssignExpression</i>, if present.</li>
  327         <li>The type of the previous <i>EnumMember</i>, if present.</li>
  328         <li><span class="d_inlinecode donthyphenate notranslate">int</span></li>
  329         </ol>
  330 <div class="blankline"></div>
  331 <div class="blankline"></div>
  332 <pre class="d_code notranslate"><span class="d_keyword">enum</span> { A, B, C }  <span class="d_comment">// anonymous enum
  333 </span></pre>
  334 <div class="blankline"></div>
  335         <p>Defines the constants A=0, B=1, C=2, all of type <span class="d_inlinecode donthyphenate notranslate">int</span>.</p>
  336 <div class="blankline"></div>
  337         <p>Enums must have at least one member.
  338         </p>
  339 <div class="blankline"></div>
  340         <p>The value of an <i>EnumMember</i> is given by its <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>.
  341         If there is no <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a> and it is the first <i>EnumMember</i>,
  342         its value is the <span class="d_inlinecode donthyphenate notranslate">.init</span> property of the <i>EnumMember</i>'s type.
  343         </p>
  344 <div class="blankline"></div>
  345         <p>If there is no <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a> and it is not the first <i>EnumMember</i>,
  346         it is given the value of the previous <i>EnumMember</i><span class="d_inlinecode donthyphenate notranslate">+1</span>.
  347         If the value of the previous <i>EnumMember</i> is the <span class="d_inlinecode donthyphenate notranslate">.max</span> property
  348         if the previous <i>EnumMember</i>'s type,
  349         it is an error.
  350         If the value of the previous <i>EnumMember</i><span class="d_inlinecode donthyphenate notranslate">+1</span> is the same as the
  351         value of the previous <i>EnumMember</i>, it is an error. (This can happen
  352         with floating point types.)
  353         </p>
  354 <div class="blankline"></div>
  355         <p>All <i>EnumMember</i>s are in scope for the <a href="../spec/expression.html#AssignExpression"><i>AssignExpression</i></a>s.
  356         </p>
  357 <div class="blankline"></div>
  358 <pre class="d_code notranslate"><span class="d_keyword">enum</span> { A, B = 5+7, C, D = 8+C, E }
  359 </pre>
  360 <div class="blankline"></div>
  361         <p>Sets A=0, B=12, C=13, D=21, and E=22, all of type <span class="d_inlinecode donthyphenate notranslate">int</span>.</p>
  362 <div class="blankline"></div>
  363 <pre class="d_code notranslate"><span class="d_keyword">enum</span> : <span class="d_keyword">long</span> { A = 3, B }
  364 </pre>
  365 <div class="blankline"></div>
  366         <p>Sets A=3, B=4 all of type <span class="d_inlinecode donthyphenate notranslate">long</span>.</p>
  367 <div class="blankline"></div>
  368 <pre class="d_code notranslate"><span class="d_keyword">enum</span> : string
  369 {
  370     A = <span class="d_string">"hello"</span>,
  371     B = <span class="d_string">"betty"</span>,
  372     C     <span class="d_comment">// error, cannot add 1 to "betty"
  373 </span>}
  374 </pre>
  375 <div class="blankline"></div>
  376 <pre class="d_code notranslate"><span class="d_keyword">enum</span>
  377 {
  378     A = 1.2f,  <span class="d_comment">// A is 1.2f of type float
  379 </span>    B,         <span class="d_comment">// B is 2.2f of type float
  380 </span>    <span class="d_keyword">int</span> C = 3, <span class="d_comment">// C is 3 of type int
  381 </span>    D          <span class="d_comment">// D is 4 of type int
  382 </span>}
  383 </pre>
  384 <div class="blankline"></div>
  385 <h2><a class="anchor" title="Permalink to this section" id="manifest_constants" href="#manifest_constants">Manifest Constants</a></h2>
  386 <div class="blankline"></div>
  387         <p>If there is only one member of an anonymous enum, the <span class="d_inlinecode donthyphenate notranslate">{ }</span> can
  388         be omitted. Gramatically speaking, this is an <a href="../spec/declaration.html#AutoDeclaration"><i>AutoDeclaration</i></a>.
  389         </p>
  390 <div class="blankline"></div>
  391 <pre class="d_code notranslate"><span class="d_keyword">enum</span> i = 4;      <span class="d_comment">// i is 4 of type int
  392 </span><span class="d_keyword">enum</span> <span class="d_keyword">long</span> l = 3; <span class="d_comment">// l is 3 of type long
  393 </span></pre>
  394 <div class="blankline"></div>
  395         <p>Manifest constants are not lvalues, meaning their address
  396         cannot be taken.  They exist only in the memory of the compiler.</p>
  397 <div class="blankline"></div>
  398 <pre class="d_code notranslate"><span class="d_keyword">enum</span> size = <span class="d_keyword">__traits</span>(classInstanceSize, Foo);  <span class="d_comment">// evaluated at compile-time
  399 </span></pre>
  400 <div class="blankline"></div>
  401         <p>The initializer for a manifest constant is evaluated using compile time function evaluation.</p>
  402 <div class="blankline"></div>
  403 <pre class="d_code notranslate"><span class="d_keyword">template</span> Foo(T)
  404 {
  405     <span class="d_comment">// Not bad, but the 'size' variable will be located in the executable.
  406 </span>    <span class="d_keyword">const</span> size_t size = T.sizeof;       <span class="d_comment">// evaluated at compile-time
  407 </span>
  408     <span class="d_comment">// ... use of 'size' at compile time ...
  409 </span>}
  410 
  411 <span class="d_keyword">template</span> Bar(T)
  412 {
  413     <span class="d_comment">// Better, the manifest constant has no runtime location in the executable.
  414 </span>    <span class="d_keyword">enum</span> size_t size = T.sizeof;        <span class="d_comment">// evaluated at compile-time
  415 </span>
  416     <span class="d_comment">// ... use of 'size' at compile time ...
  417 </span>
  418     <span class="d_comment">// If you take the address of Foo!T.size, it would also go into exe file.
  419 </span>    <span class="d_keyword">auto</span> p = &amp;Foo!T.size;
  420 }
  421 </pre>
  422 <div class="blankline"></div>
  423 <div class="blankline"></div>
  424 
  425 <div style="float: left"><i class="fa fa-angle-left" aria-hidden="true"></i> <a href="../spec/interface.html">Interfaces</a></div>
  426 <div style="float: right"><a href="../spec/const3.html">Type Qualifiers</a> <i class="fa fa-angle-right" aria-hidden="true"></i></div>
  427 <div style="clear:both"></div>
  428 
  429 
  430 
  431         <div class="smallprint" id="copyright">Copyright &copy; 1999-2020 by the <a href="../foundation_overview.html">D Language Foundation</a> | Page generated by
  432 <a href="../spec/ddoc.html">Ddoc</a> on Fri Nov 20 21:58:02 2020
  433 </div>
  434     </div>
  435 </div>
  436 
  437     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  438     <script type="text/javascript">window.jQuery || document.write('\x3Cscript src="../js/jquery-1.7.2.min.js">\x3C/script>');</script>
  439     <script type="text/javascript" src="../js/dlang.js"></script>
  440     
  441     <script type="text/javascript" src="../js/codemirror-compressed.js"></script>
  442     <script type="text/javascript" src="../js/run.js"></script>
  443 
  444 
  445 <script type="text/javascript" src="../js/listanchors.js"></script>
  446 <script type="text/javascript">jQuery(document).ready(addVersionSelector);</script>
  447 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
  448 </body>
  449 </html>