better-enums/tutorial/SafeSwitch.html
2020-10-19 09:11:42 +03:00

135 lines
3.9 KiB
HTML

<!-- Generated automatically - edit the templates! -->
<!DOCTYPE html>
<html>
<head>
<title>Safe switch - Better Enums</title>
<link rel="canonical" href="http://aantron.github.io/better-enums/tutorial/SafeSwitch.html" />
<meta name="description" content="Better Enums can be used directly in switch statements like
normal enums, making it possible for the compiler to check that all cases are
listed, increasing the safety of your code." />
<meta name="author" content="Anton Bachin" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="../better-enums.css" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-62962513-1', 'auto');
ga('send', 'pageview');
</script>
</head>
<body class="">
<nav>
<div class="container">
<a class="first" href="https://raw.githubusercontent.com/aantron/better-enums/0.11.3/enum.h"
download>Download</a>
<a href="https://github.com/aantron/better-enums">GitHub</a>
<a href="../index.html">Home</a>
<a href="../tutorial/HelloWorld.html">Tutorial</a>
<a href="../ApiReference.html">Reference</a>
</div>
</nav>
<div class="spacer">&nbsp;</div>
<header>
<div class="container">
<section>
<h1><a href="../index.html">Better Enums</a></h1>
<h2>Reflective compile-time enums for <span class="cpp">C++</span></h2>
<h3>Open-source under the BSD license</h3>
</section>
<section class="notes">
<p>Version 0.11.3</p>
<p>To install, just add <code>enum.h</code> to your project.</p>
<p>
Visit the GitHub repo for issues, feedback, and the latest development.
</p>
</section>
<section class="buttons">
<a href="https://raw.githubusercontent.com/aantron/better-enums/0.11.3/enum.h"
download>Download <code>enum.h</code></a>
<a href="https://github.com/aantron/better-enums">GitHub</a>
</section>
</div>
</header>
<div class="main">
<div class="container">
<p>
Welcome to the Better Enums tutorials! The code in this tutorial forms a
valid program, which you can <a href="https://github.com/aantron/better-enums/blob/0.11.3/example/4-switch.cc">download</a> and play with. The
program runs as part of the automated test suite.
</p>
<h2>Safe switch</h2>
<p>A Better Enum can be used directly in a <code>switch</code> statement:</p>
<pre>#include &lt;iostream&gt;
<em>#include &lt;enum.h&gt;</em>
<em>BETTER_ENUM(Channel, int, Red, Green, Blue)</em>
int main()
{
Channel channel = Channel::Green;
int n;
<em>switch </em>(<em>channel</em>) {
<em>case Channel::Red</em>: n = 13; break;
<em>case Channel::Green</em>: n = 37; break;
<em>case Channel::Blue</em>: n = 42; break;
}</pre><p>If you miss a case or add a redundant one, your compiler should be able to give
you a warning &mdash; try it!</p>
<p>Note that on msvc, you may need to enable <a href="https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4062">warning C4062</a>.</p>
<hr>
<pre> std::cout &lt;&lt; n &lt;&lt; std::endl;
return 0;
}</pre>
<section class="tutorial-footer">
<p class="next">
Continue to the next tutorial: <a href="../tutorial/Maps.html">Maps</a>
</p>
<p class="up">
Or, return to the
<a href="../index.html#Tutorial">tutorial index</a>.
</p>
</section>
</div>
</div>
<footer>
<div class="container">
Copyright &copy; 2015-2019 Anton Bachin. Released under the BSD 2-clause
license. See
<a href="https://github.com/aantron/better-enums/blob/0.11.3/doc/LICENSE">
LICENSE</a>.
<br />
This page is part of the documentation for Better Enums 0.11.3.
</div>
</footer>
</body>
</html>