mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-06 08:46:42 +08:00
149 lines
4.9 KiB
HTML
149 lines
4.9 KiB
HTML
<!-- Generated automatically - edit the templates! -->
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
<head>
|
|
|
|
<title>Compile-time usage - Better Enums</title>
|
|
|
|
<link rel="canonical" href="http://aantron.github.io/better-enums/tutorial/CompileTimeUsage.html" />
|
|
<meta name="description" content="Better Enums can be used entirely at compile time in C++11. All
|
|
conversion functions are available for constexpr functions or templates." />
|
|
<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"> </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/9-constexpr.cc">download</a> and play with. The
|
|
program runs as part of the automated test suite.
|
|
</p>
|
|
|
|
<h2>Compile-time usage</h2>
|
|
<p>When used with <span class="cpp">C++</span><span class="eleven">11</span>, Better Enums are generated entirely during compilation.
|
|
All the data is available for use by your own <code>constexpr</code> functions. The
|
|
examples in <em>this</em> tutorial aren't very useful, but look at the
|
|
<a href="../index.html#CompileTimeDemos">demos</a> at the bottom of the main page to
|
|
get an idea of what can be done. Here, you will see the basics.</p>
|
|
<pre>#include <iostream>
|
|
|
|
// The reason for this is explained below.
|
|
#ifndef BETTER_ENUMS_CONSTEXPR_TO_STRING
|
|
#define BETTER_ENUMS_CONSTEXPR_TO_STRING
|
|
#endif
|
|
|
|
<em>#include <enum.h></em>
|
|
|
|
<em>BETTER_ENUM</em>(<em>Channel</em>, <em>int</em>, <em>Red</em> = <em>1</em>, <em>Green</em> = <em>2</em>, <em>Blue</em> = <em>3</em>)
|
|
|
|
<em>constexpr</em> Channel channel = <em>Channel::_from_integral(2)</em>;
|
|
<em>constexpr</em> int value = <em>channel._to_integral()</em>;
|
|
|
|
<em>constexpr</em> const char *name = <em>channel._to_string()</em>;
|
|
<em>constexpr</em> Channel parsed = <em>Channel::_from_string("Red")</em>;</pre><p>All of the above are computed during compilation. The reason for the macro
|
|
definition at the top of the file is explained on the
|
|
<a href="../OptInFeatures.html#CompileTimeNameTrimming">opt-in features page</a>.
|
|
Basically, it makes <code>_to_string</code> <code>constexpr</code>, but slows down compilation.</p>
|
|
<p>You can also do things such as:</p>
|
|
<pre><em>constexpr size_t length</em>(<em>const char *s</em>, <em>size_t index = 0</em>)
|
|
{
|
|
return <em>s[index] == '\0'</em> ? <em>index</em> : <em>length(s, index + 1)</em>;
|
|
}
|
|
|
|
<em>constexpr</em> size_t <em>length_of_name_of_second_constant</em> =
|
|
<em>length(Channel::_names()[1])</em>;
|
|
|
|
int main()
|
|
{
|
|
std::cout << <em>length_of_name_of_second_constant</em> << std::endl;
|
|
|
|
return 0;
|
|
}</pre><p>Which prints "5", the length of "Green". That 5 was also computed during
|
|
compilation.</p>
|
|
|
|
|
|
<section class="tutorial-footer">
|
|
<p class="up">
|
|
This is the last tutorial! Return to the
|
|
<a href="../index.html">main page</a> for other resources.
|
|
</p>
|
|
</section>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<footer>
|
|
<div class="container">
|
|
Copyright © 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>
|
|
|