<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 849: missing type traits to compute root class and derived class of types in a class hierachy</title>
<meta property="og:title" content="Issue 849: missing type traits to compute root class and derived class of types in a class hierachy">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue849.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#NAD">NAD</a> status.</em></p>
<h3 id="849"><a href="lwg-closed.html#849">849</a>. missing type traits to compute root class and derived class of types in a class hierachy</h3>
<p><b>Section:</b> 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Thorsten Ottosen <b>Opened:</b> 2008-06-05 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#meta.trans.other">issues</a> in [meta.trans.other].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The type traits library contains various traits to dealt with
polymorphic types, e.g. <code>std::has_virtual_destructor</code>, <code>std::is_polymorphic</code>
and <code>std::is_base_of</code>. However, there is no way to compute the unique
public base class of a type  if such  one exists.  Such a trait could be
very useful if one needs to instantiate a specialization made for the
root class whenever a derived class is passed as parameter. For example,
imagine that you wanted to specialize <code>std::hash</code> for a class
hierarchy---instead of specializing each class, you could specialize the
<code>std::hash&lt;root_class&gt;</code> and provide a partial specialization that worked
for all derived classes.
</p>

<p>
This ability---to specify operations in terms of their equivalent in the
root class---can be done with e.g. normal functions, but there is,
AFAIK, no way to do it for class templates. Being able to access
compile-time information about the type-hierachy can be very powerful,
and I therefore also suggest traits that computes the directly derived
class whenever that is possible.
</p>

<p>
If the computation can not be done, the traits should fall back on an
identity transformation. I expect this gives the best overall usability.
</p>


<p id="res-849"><b>Proposed resolution:</b></p>
<p>
Add the following to the synopsis in 21.3.3 <a href="https://wg21.link/meta.type.synop">[meta.type.synop]</a> under "other transformations":
</p>

<blockquote><pre>
template&lt; class T &gt; struct direct_base_class;
template&lt; class T &gt; struct direct_derived_class;
template&lt; class T &gt; struct root_base_class;
</pre></blockquote>

<p>
Add three new entries to table 51 (21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a>) with the following content
</p>

<blockquote>
<table border="1">
<tr>
<th>Template</th><th>Condition</th><th>Comments</th>
</tr>
<tr>
<td><code>template&lt; class T &gt; struct direct_base_class;</code></td>
<td><code>T</code> shall be a complete type.</td>
<td>The member typedef <code>type</code> shall equal the accessible unambiguous direct base class of <code>T</code>.
If no such type exists, the member typedef <code>type</code> shall equal <code>T</code>.</td>
</tr>
<tr>
<td><code>template&lt; class T &gt; struct direct_derived_class;</code></td>
<td><code>T</code> shall be a complete type.</td>
<td>The member typedef <code>type</code> shall equal the unambiguous type which has <code>T</code>
as an accessible unambiguous direct base class. If no such type exists, the member typedef
<code>type</code> shall equal <code>T</code>.</td>
</tr>
<tr>
<td><code>template&lt; class T &gt; struct root_base_class;</code></td>
<td><code>T</code> shall be a complete type.</td>
<td>The member typedef <code>type</code> shall equal the accessible unambiguous most indirect base class of
<code>T</code>. If no such type exists, the member typedef type shall equal <code>T</code>.</td>
</tr>
</table>
</blockquote>



<p><b>Rationale:</b></p><p>
2008-9-16 San Francisco:  Issue pulled by author prior to being reviewed by the LWG.
</p>




</body>
</html>
