<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 906: ObjectType is the wrong concept to constrain initializer_list</title>
<meta property="og:title" content="Issue 906: ObjectType is the wrong concept to constrain initializer_list">
<meta property="og:description" content="C++ library issue. Status: NAD Concepts">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue906.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_Concepts">NAD Concepts</a> status.</em></p>
<h3 id="906"><a href="lwg-closed.html#906">906</a>. <code>ObjectType</code> is the wrong concept to constrain <code>initializer_list</code></h3>
<p><b>Section:</b> 17.11 <a href="https://wg21.link/support.initlist">[support.initlist]</a> <b>Status:</b> <a href="lwg-active.html#NAD_Concepts">NAD Concepts</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2008-09-26 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#support.initlist">active issues</a> in [support.initlist].</p>
<p><b>View all other</b> <a href="lwg-index.html#support.initlist">issues</a> in [support.initlist].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD Concepts">NAD Concepts</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The currently proposed constraint on <code>initializer_list</code>'s element type
<code>E</code> is that is has to meet <code>ObjectType</code>. This is an underspecification,
because both core language and library part of <code>initializer_list</code>
make clear, that it references an implicitly allocated array:
</p>
<p>
9.5.5 <a href="https://wg21.link/dcl.init.list">[dcl.init.list]</a>/4:
</p>
<blockquote><p>
When an initializer list is implicitly converted to a
<code>std::initializer_list&lt;E&gt;</code>, the object passed is constructed as if the
implementation allocated an array of N elements of type <code>E</code>, where
N is the number of elements in the initializer list.[..]
</p></blockquote>

<p>
17.11 <a href="https://wg21.link/support.initlist">[support.initlist]</a>/2.
</p>

<blockquote><p>
An object of type <code>initializer_list&lt;E&gt;</code> provides access to an array of
objects of type <code>const E</code>.[..]
</p></blockquote>

<p>
Therefore, <code>E</code> needs to fulfill concept <code>ValueType</code> (thus excluding
abstract class types). This stricter requirement should be added
to prevent deep instantiation errors known from the bad old times,
as shown in the following example:
</p>

<blockquote><pre>
// Header A: (Should concept-check even in stand-alone modus)

template &lt;DefaultConstructible T&gt;
requires MoveConstructible&lt;T&gt;
void generate_and_do_3(T a) {
  std::initializer_list&lt;T&gt; list{T(), std::move(a), T()};
  ...
}

void do_more();
void do_more_or_less();

template &lt;DefaultConstructible T&gt;
requires MoveConstructible&lt;T&gt;
void more_generate_3() {
  do_more();
  generate_and_do_3(T());
}

template &lt;DefaultConstructible T&gt;
requires MoveConstructible&lt;T&gt;
void something_and_generate_3() {
  do_more_or_less();
  more_generate_3();
}

// Test.cpp

#include "A.h"

class Abstract {
public:
  virtual ~Abstract();
  virtual void foo() = 0; // abstract type
  Abstract(Abstract&amp;&amp;){} // MoveConstructible
  Abstract(){} // DefaultConstructible
};

int main() {
  // The restricted template *accepts* the argument, but
  // causes a deep instantiation error in the internal function
  // generate_and_do_3:
  something_and_generate_3&lt;Abstract&gt;();
}
</pre></blockquote>

<p>
The proposed stricter constraint does not minimize the aim to
support more general containers for which <code>ObjectType</code> would be
sufficient. If such an extended container (lets assume it's still a
class template) provides a constructor that accepts an <code>initializer_list</code>
only <em>this</em> constructor would need to be restricted on <code>ValueType</code>:
</p>

<blockquote><pre>
template&lt;ObjectType T&gt;
class ExtContainer {
public:
  requires ValueType&lt;T&gt;
  ExtContainer(std::initializer_list&lt;T&gt;);
  ...
};
</pre></blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote><p>
Move to Tentatively Ready.
</p></blockquote>

<p><i>[
2009-07 Frankfurt:
]</i></p>


<blockquote><p>
Need to look at again without concepts.
</p></blockquote>



<p id="res-906"><b>Proposed resolution:</b></p>
<ol>
<li>
In 17.11 <a href="https://wg21.link/support.initlist">[support.initlist]</a>/p.1 replace in "header <code>&lt;initializer_list&gt;</code> synopsis"
the constraint "<code>ObjectType</code>" in the template parameter list by the
constraint "<code>ValueType</code>".
</li>
</ol>






</body>
</html>
