|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4207" status="New"> |
| 5 | +<title>Point of reference for `source_location` is not specified when used in an default template argument</title> |
| 6 | +<section><sref ref="[support.srcloc.cons]"/></section> |
| 7 | +<submitter>Cassio Neri</submitter> |
| 8 | +<date>07 Feb 2025</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +<sref ref="[support.srcloc.cons]"/>/2 in <paper num="N5001"/> says nothing |
| 14 | +about using `source_location::current()` in an initializer of a template |
| 15 | +parameter. The example below suggests that gcc, clang and msvc seem to agree |
| 16 | +on this case. It would be nice if the Standard legitimized current practice. |
| 17 | +</p> |
| 18 | +<pre><code> |
| 19 | +#include <source_location> |
| 20 | + |
| 21 | +template <int i = std::source_location::current().line()> // line 3 |
| 22 | +struct A { |
| 23 | + static constexpr int value = i; |
| 24 | +}; |
| 25 | + |
| 26 | +template <int i = std::source_location::current().line()> // line 8 |
| 27 | +constexpr int f() { |
| 28 | + return i; |
| 29 | +} |
| 30 | + |
| 31 | +static_assert(A<>::value == 3); // passes |
| 32 | +static_assert(f() == 8); // passes |
| 33 | +</code></pre> |
| 34 | + |
| 35 | +<note>2025-02-07; Jonathan provides wording</note> |
| 36 | +<p> |
| 37 | +For a default argument of a function parameter, `current()` is recommended |
| 38 | +to return the location of the caller that makes use of that default argument. |
| 39 | +For a default template argument, the location would be determined by the |
| 40 | +template's point of instantiation (<sref ref="[temp.point]"/>) which would |
| 41 | +not always do what users expect. Using the location of the default template |
| 42 | +argument in the template declaration seems sensible and predictable, |
| 43 | +and matches existing practice. Arguably, this doesn't need to be stated |
| 44 | +because it's just "exactly where the `current()` call appear in the source". |
| 45 | +The other cases in the Remarks paragraph are situations where the preferred |
| 46 | +location is different, because we want to know where it's used, not defined. |
| 47 | +</p> |
| 48 | +</discussion> |
| 49 | + |
| 50 | +<resolution> |
| 51 | +<p> |
| 52 | +This wording is relative to <paper num="N5001"/>. |
| 53 | +</p> |
| 54 | + |
| 55 | +<ol> |
| 56 | +<li> |
| 57 | +Modify <sref ref="[support.srcloc.cons]"/> as indicated: |
| 58 | +<blockquote> |
| 59 | +-2- <i>Remarks</i>: |
| 60 | +Any call to `current` that appears as a default member initializer |
| 61 | +(<sref ref="[class.mem]"/>), or as a subexpression thereof, |
| 62 | +should correspond to the location of the constructor definition |
| 63 | +or aggregate initialization that uses the default member initializer. |
| 64 | +Any call to `current` that appears as a default argument |
| 65 | +(<sref ref="[dcl.fct.default]"/>), or as a subexpression thereof, |
| 66 | +should correspond to the location of the invocation of the function that |
| 67 | +uses the default argument (<sref ref="[expr.call]"/>). |
| 68 | +Any call to `current` that appears as a default template argument |
| 69 | +(<sref ref="[temp.param]"/>), or as a subexpression thereof, |
| 70 | +should correspond to the location where the default template argument |
| 71 | +is specified. |
| 72 | +</blockquote> |
| 73 | +</li> |
| 74 | +</ol> |
| 75 | +</resolution> |
| 76 | + |
| 77 | +</issue> |
0 commit comments