2 Copyright Louis Dionne 2013-2017
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
6 <!-- boost-no-inspect -->
7 <!-- HTML header for doxygen 1.8.9.1-->
8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
9 <html xmlns="http://www.w3.org/1999/xhtml">
11 <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
12 <meta http-equiv="X-UA-Compatible" content="IE=9"/>
13 <meta name="generator" content="Doxygen 1.8.11"/>
14 <title>Boost.Hana: boost::hana::tag_of< T, enabler > Struct Template Reference</title>
15 <link href="tabs.css" rel="stylesheet" type="text/css"/>
16 <script type="text/javascript" src="jquery.js"></script>
17 <script type="text/javascript" src="dynsections.js"></script>
18 <link href="navtree.css" rel="stylesheet" type="text/css"/>
19 <script type="text/javascript" src="resize.js"></script>
20 <script type="text/javascript" src="navtreedata.js"></script>
21 <script type="text/javascript" src="navtree.js"></script>
22 <script type="text/javascript">
23 $(document).ready(initResizable);
24 $(window).load(resizeHeight);
26 <link href="search/search.css" rel="stylesheet" type="text/css"/>
27 <script type="text/javascript" src="search/searchdata.js"></script>
28 <script type="text/javascript" src="search/search.js"></script>
29 <script type="text/javascript">
30 $(document).ready(function() { init_search(); });
32 <script type="text/x-mathjax-config">
34 extensions: ["tex2jax.js"],
35 jax: ["input/TeX","output/HTML-CSS"],
37 // Copyright Louis Dionne 2013-2017
38 // Distributed under the Boost Software License, Version 1.0.
39 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
44 width: "75% container"
48 </script><script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
49 <link href="doxygen.css" rel="stylesheet" type="text/css" />
50 <!-- Additional javascript for drawing charts. -->
51 <script type="text/javascript" src="highcharts.js"></script>
52 <script type="text/javascript" src="highcharts-data.js"></script>
53 <script type="text/javascript" src="highcharts-exporting.js"></script>
54 <script type="text/javascript" src="chart.js"></script>
55 <script type="text/javascript" src="hana.js"></script>
58 <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
60 <table cellspacing="0" cellpadding="0">
62 <tr style="height: 56px;">
63 <td id="projectlogo"><img alt="Logo" src="Boost.png"/></td>
64 <td style="padding-left: 0.5em;">
65 <div id="projectname">Boost.Hana
66  <span id="projectnumber">1.1.0</span>
68 <div id="projectbrief">Your standard library for metaprogramming</div>
70 <td> <div id="MSearchBox" class="MSearchBoxInactive">
72 <img id="MSearchSelect" src="search/mag_sel.png"
73 onmouseover="return searchBox.OnSearchSelectShow()"
74 onmouseout="return searchBox.OnSearchSelectHide()"
76 <input type="text" id="MSearchField" value="Search" accesskey="S"
77 onfocus="searchBox.OnSearchFieldFocus(true)"
78 onblur="searchBox.OnSearchFieldFocus(false)"
79 onkeyup="searchBox.OnSearchFieldChange(event)"/>
80 </span><span class="right">
81 <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
89 <!-- end header part -->
90 <!-- Generated by Doxygen 1.8.11 -->
91 <script type="text/javascript">
92 var searchBox = new SearchBox("searchBox", "search",false,'Search');
95 <div id="side-nav" class="ui-resizable side-nav-resizable">
97 <div id="nav-tree-contents">
98 <div id="nav-sync" class="sync"></div>
101 <div id="splitbar" style="-moz-user-select:none;"
102 class="ui-resizable-handle">
105 <script type="text/javascript">
106 $(document).ready(function(){initNavTree('structboost_1_1hana_1_1tag__of.html','');});
108 <div id="doc-content">
109 <!-- window showing the filter options -->
110 <div id="MSearchSelectWindow"
111 onmouseover="return searchBox.OnSearchSelectShow()"
112 onmouseout="return searchBox.OnSearchSelectHide()"
113 onkeydown="return searchBox.OnSearchSelectKey(event)">
116 <!-- iframe showing the search results (closed by default) -->
117 <div id="MSearchResultsWindow">
118 <iframe src="javascript:void(0)" frameborder="0"
119 name="MSearchResults" id="MSearchResults">
124 <div class="headertitle">
125 <div class="title">boost::hana::tag_of< T, enabler > Struct Template Reference<div class="ingroups"><a class="el" href="group__group-core.html">Core</a></div></div> </div>
127 <div class="contents">
128 <a name="details" id="details"></a><h2 class="groupheader">Description</h2>
129 <div class="textblock"><h3>template<typename T, optional when-based enabler><br />
130 struct boost::hana::tag_of< T, enabler ></h3>
132 <p>Metafunction returning the tag associated to <code>T</code>. </p>
133 <p>There are several ways to specify the tag of a C++ type. If it's a user-defined type, one can define a nested <code>hana_tag</code> alias: </p><div class="fragment"><div class="line"><span class="keyword">struct </span>MyUserDefinedType {</div><div class="line"> <span class="keyword">using</span> hana_tag = MyTag;</div><div class="line">};</div></div><!-- fragment --><p>Sometimes, however, the C++ type can't be modified (if it's in a foreign library) or simply can't have nested types (if it's not a struct or class). In those cases, using a nested alias is impossible and so ad-hoc customization is also supported by specializing <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a></code> in the <code><a class="el" href="namespaceboost_1_1hana.html" title="Namespace containing everything in the library. ">boost::hana</a></code> namespace: </p><div class="fragment"><div class="line"><span class="keyword">struct </span>i_cant_modify_this;</div><div class="line"></div><div class="line"><span class="keyword">namespace </span><a class="code" href="namespaceboost.html">boost</a> { <span class="keyword">namespace </span>hana {</div><div class="line"> <span class="keyword">template</span> <></div><div class="line"> <span class="keyword">struct </span>tag_of<i_cant_modify_this> {</div><div class="line"> <span class="keyword">using</span> type = MyTag;</div><div class="line"> };</div><div class="line">}}</div></div><!-- fragment --><p><code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a></code> can also be specialized for all C++ types satisfying some boolean condition using <code>when</code>. <code>when</code> accepts a single compile-time boolean and enables the specialization of <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a></code> if and only if that boolean is <code>true</code>. This is similar to the well known C++ idiom of using a dummy template parameter with <code>std::enable_if</code> and relying on SFINAE. For example, we could specify the tag of all <code><a class="el" href="structboost_1_1fusion_1_1vector.html" title="Adapter for Boost.Fusion vectors. ">fusion::vector</a></code>s by doing: </p><div class="fragment"><div class="line"><span class="keyword">struct </span>BoostFusionVector;</div><div class="line"></div><div class="line"><span class="keyword">namespace </span><a class="code" href="namespaceboost.html">boost</a> { <span class="keyword">namespace </span>hana {</div><div class="line"> <span class="keyword">template</span> <<span class="keyword">typename</span> T></div><div class="line"> <span class="keyword">struct </span>tag_of<T, when<</div><div class="line"> <a class="code" href="namespacestd.html">std</a>::is_same<</div><div class="line"> typename fusion::traits::tag_of<T>::type,</div><div class="line"> fusion::traits::tag_of<fusion::vector<>>::type</div><div class="line"> >{}</div><div class="line"> >> {</div><div class="line"> <span class="keyword">using</span> type = BoostFusionVector;</div><div class="line"> };</div><div class="line">}}</div></div><!-- fragment --><p>Also, when it is not specialized and when the given C++ type does not have a nested <code>hana_tag</code> alias, <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a><T></code> returns <code>T</code> itself. This makes tags a simple extension of normal C++ types. This is <em>super</em> useful, mainly for two reasons. First, this allows Hana to adopt a reasonable default behavior for some operations involving types that have no notion of tags. For example, Hana allows comparing with <code>equal</code> any two objects for which a valid <code>operator==</code> is defined, and that without any work on the user side. Second, it also means that you can ignore tags completely if you don't need their functionality; just use the normal C++ type of your objects and everything will "just work".</p>
134 <p>Finally, also note that <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a><T></code> is always equivalent to <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a><U></code>, where <code>U</code> is the type <code>T</code> after being stripped of all references and cv-qualifiers. This makes it unnecessary to specialize <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a></code> for all reference and cv combinations, which would be a real pain. Also, <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a></code> is required to be idempotent. In other words, it must always be the case that <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a><<a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a><T>::type>::type</code> is equivalent to <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a><T>::type</code>.</p>
135 <blockquote class="doxtable">
136 <p><b>Tip 1</b><br />
137 If compile-time performance is a serious concern, consider specializing the <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a></code> metafunction in Hana's namespace. When unspecialized, the metafunction has to use SFINAE, which tends to incur a larger compile-time overhead. For heavily used templated types, this can potentially make a difference. </p>
139 <blockquote class="doxtable">
140 <p><b>Tip 2</b><br />
141 Consider using <code>tag_of_t</code> alias instead of <code><a class="el" href="structboost_1_1hana_1_1tag__of.html" title="Metafunction returning the tag associated to T. ">tag_of</a></code>, which reduces the amount of typing in dependent contexts. </p>
144 <div class="fragment"><div class="line"><span class="comment">// Copyright Louis Dionne 2013-2017</span></div><div class="line"><span class="comment">// Distributed under the Boost Software License, Version 1.0.</span></div><div class="line"><span class="comment">// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <<a class="code" href="core_2tag__of_8hpp.html">boost/hana/core/tag_of.hpp</a>></span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <type_traits></span></div><div class="line"><span class="keyword">namespace </span>hana = <a class="code" href="namespaceboost_1_1hana.html">boost::hana</a>;</div><div class="line"></div><div class="line"></div><div class="line">static_assert(std::is_same<hana::tag_of<int>::type, <span class="keywordtype">int</span>>{}, <span class="stringliteral">""</span>);</div><div class="line">static_assert(std::is_same<hana::tag_of<int&>::type, <span class="keywordtype">int</span>>{}, <span class="stringliteral">""</span>);</div><div class="line">static_assert(std::is_same<hana::tag_of<int const&>::type, <span class="keywordtype">int</span>>{}, <span class="stringliteral">""</span>);</div><div class="line"></div><div class="line"><span class="keyword">struct </span>PersonTag;</div><div class="line"><span class="keyword">struct </span>Person { <span class="keyword">using</span> hana_tag = PersonTag; };</div><div class="line">static_assert(std::is_same<hana::tag_of<Person>::type, PersonTag>{}, <span class="stringliteral">""</span>);</div><div class="line">static_assert(std::is_same<hana::tag_of<Person volatile&&>::type, PersonTag>{}, <span class="stringliteral">""</span>);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main() { }</div></div><!-- fragment --> </div></div><!-- contents -->
145 </div><!-- doc-content -->
147 Copyright Louis Dionne 2013-2017
148 Distributed under the Boost Software License, Version 1.0.
149 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
151 <!-- boost-no-inspect -->
152 <!-- HTML footer for doxygen 1.8.9.1-->
153 <!-- start footer part -->
154 <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
156 <li class="navelem"><a class="el" href="namespaceboost.html">boost</a></li><li class="navelem"><a class="el" href="namespaceboost_1_1hana.html">hana</a></li><li class="navelem"><a class="el" href="structboost_1_1hana_1_1tag__of.html">tag_of</a></li>