|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4207: Point of reference for source_location is not specified when used in an default template argument</title> |
| 6 | +<meta property="og:title" content="Issue 4207: Point of reference for source_location is not specified when used in an default template argument"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4207.html"> |
| 9 | +<meta property="og:type" content="website"> |
| 10 | +<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png"> |
| 11 | +<meta property="og:image:alt" content="C++ logo"> |
| 12 | +<style> |
| 13 | + p {text-align:justify} |
| 14 | + li {text-align:justify} |
| 15 | + pre code.backtick::before { content: "`" } |
| 16 | + pre code.backtick::after { content: "`" } |
| 17 | + blockquote.note |
| 18 | + { |
| 19 | + background-color:#E0E0E0; |
| 20 | + padding-left: 15px; |
| 21 | + padding-right: 15px; |
| 22 | + padding-top: 1px; |
| 23 | + padding-bottom: 1px; |
| 24 | + } |
| 25 | + ins {background-color:#A0FFA0} |
| 26 | + del {background-color:#FFA0A0} |
| 27 | + table.issues-index { border: 1px solid; border-collapse: collapse; } |
| 28 | + table.issues-index th { text-align: center; padding: 4px; border: 1px solid; } |
| 29 | + table.issues-index td { padding: 4px; border: 1px solid; } |
| 30 | + table.issues-index td:nth-child(1) { text-align: right; } |
| 31 | + table.issues-index td:nth-child(2) { text-align: left; } |
| 32 | + table.issues-index td:nth-child(3) { text-align: left; } |
| 33 | + table.issues-index td:nth-child(4) { text-align: left; } |
| 34 | + table.issues-index td:nth-child(5) { text-align: center; } |
| 35 | + table.issues-index td:nth-child(6) { text-align: center; } |
| 36 | + table.issues-index td:nth-child(7) { text-align: left; } |
| 37 | + table.issues-index td:nth-child(5) span.no-pr { color: red; } |
| 38 | + @media (prefers-color-scheme: dark) { |
| 39 | + html { |
| 40 | + color: #ddd; |
| 41 | + background-color: black; |
| 42 | + } |
| 43 | + ins { |
| 44 | + background-color: #225522 |
| 45 | + } |
| 46 | + del { |
| 47 | + background-color: #662222 |
| 48 | + } |
| 49 | + a { |
| 50 | + color: #6af |
| 51 | + } |
| 52 | + a:visited { |
| 53 | + color: #6af |
| 54 | + } |
| 55 | + blockquote.note |
| 56 | + { |
| 57 | + background-color: rgba(255, 255, 255, .10) |
| 58 | + } |
| 59 | + } |
| 60 | +</style> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | +<hr> |
| 64 | +<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#New">New</a> status.</em></p> |
| 65 | +<h3 id="4207"><a href="lwg-active.html#4207">4207</a>. Point of reference for <code class='backtick'>source_location</code> is not specified when used in an default template argument</h3> |
| 66 | +<p><b>Section:</b> 17.8.2.2 <a href="https://wg21.link/support.srcloc.cons">[support.srcloc.cons]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Cassio Neri <b>Opened:</b> 2025-02-07 <b>Last modified:</b> 2025-02-07</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View all other</b> <a href="lwg-index.html#support.srcloc.cons">issues</a> in [support.srcloc.cons].</p> |
| 71 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 72 | +<p><b>Discussion:</b></p> |
| 73 | +<p> |
| 74 | +17.8.2.2 <a href="https://wg21.link/support.srcloc.cons">[support.srcloc.cons]</a>/2 in <a href="https://wg21.link/N5001">N5001</a> says nothing |
| 75 | +about using <code class='backtick'>source_location::current()</code> in an initializer of a template |
| 76 | +parameter. The example below suggests that gcc, clang and msvc seem to agree |
| 77 | +on this case. It would be nice if the Standard legitimized current practice. |
| 78 | +</p> |
| 79 | +<pre><code> |
| 80 | +#include <source_location> |
| 81 | + |
| 82 | +template <int i = std::source_location::current().line()> // line 3 |
| 83 | +struct A { |
| 84 | + static constexpr int value = i; |
| 85 | +}; |
| 86 | + |
| 87 | +template <int i = std::source_location::current().line()> // line 8 |
| 88 | +constexpr int f() { |
| 89 | + return i; |
| 90 | +} |
| 91 | + |
| 92 | +static_assert(A<>::value == 3); // passes |
| 93 | +static_assert(f() == 8); // passes |
| 94 | +</code></pre> |
| 95 | + |
| 96 | +<p><i>[2025-02-07; Jonathan provides wording]</i></p> |
| 97 | + |
| 98 | +<p> |
| 99 | +For a default argument of a function parameter, <code class='backtick'>current()</code> is recommended |
| 100 | +to return the location of the caller that makes use of that default argument. |
| 101 | +For a default template argument, the location would be determined by the |
| 102 | +template's point of instantiation (13.8.4.1 <a href="https://wg21.link/temp.point">[temp.point]</a>) which would |
| 103 | +not always do what users expect. Using the location of the default template |
| 104 | +argument in the template declaration seems sensible and predictable, |
| 105 | +and matches existing practice. Arguably, this doesn't need to be stated |
| 106 | +because it's just "exactly where the <code class='backtick'>current()</code> call appear in the source". |
| 107 | +The other cases in the Remarks paragraph are situations where the preferred |
| 108 | +location is different, because we want to know where it's used, not defined. |
| 109 | +</p> |
| 110 | + |
| 111 | + |
| 112 | +<p id="res-4207"><b>Proposed resolution:</b></p> |
| 113 | +<p> |
| 114 | +This wording is relative to <a href="https://wg21.link/N5001">N5001</a>. |
| 115 | +</p> |
| 116 | + |
| 117 | +<ol> |
| 118 | +<li> |
| 119 | +Modify 17.8.2.2 <a href="https://wg21.link/support.srcloc.cons">[support.srcloc.cons]</a> as indicated: |
| 120 | +<blockquote> |
| 121 | +-2- <i>Remarks</i>: |
| 122 | +Any call to <code class='backtick'>current</code> that appears as a default member initializer |
| 123 | +(11.4 <a href="https://wg21.link/class.mem">[class.mem]</a>), or as a subexpression thereof, |
| 124 | +should correspond to the location of the constructor definition |
| 125 | +or aggregate initialization that uses the default member initializer. |
| 126 | +Any call to <code class='backtick'>current</code> that appears as a default argument |
| 127 | +(9.3.4.7 <a href="https://wg21.link/dcl.fct.default">[dcl.fct.default]</a>), or as a subexpression thereof, |
| 128 | +should correspond to the location of the invocation of the function that |
| 129 | +uses the default argument (7.6.1.3 <a href="https://wg21.link/expr.call">[expr.call]</a>). |
| 130 | +Any call to <code class='backtick'>current</code> that appears as a default template argument |
| 131 | +(13.2 <a href="https://wg21.link/temp.param">[temp.param]</a>), or as a subexpression thereof, |
| 132 | +should correspond to the location where the default template argument |
| 133 | +is specified. |
| 134 | +</blockquote> |
| 135 | +</li> |
| 136 | +</ol> |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +</body> |
| 143 | +</html> |
0 commit comments