<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62462>62462</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Temporary `std::string` in template argument is not considert a constant expression in C++20 mode
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bebuch
      </td>
    </tr>
</table>

<pre>
    This is a bug in `clang++`. I tested with `libc++` and `libstdc++` which both give similar error messages.

```cpp
#include <algorithm>
#include <string>

template <std::size_t N>
struct ct_to_rt_string {
 consteval ct_to_rt_string(std::string const str) {
 std::copy_n(str.c_str(), N + 1, buffer);
    }

    char buffer[N + 1];
};

constexpr std::string make_ct_std_string() {
    return "compile time text";
}

#include <iostream>

int main() {
    std::cout << ct_to_rt_string<make_ct_std_string().size()>(make_ct_std_string()).buffer;
}
```

GCC 12.2 compiles this code and prints `compile time text` as expected.

`clang++ -std=c++20 -stdlib=libc++` fails to compile with:

```
<source>:20:34: error: non-type template argument is not a constant expression
    std::cout << ct_to_rt_string<make_ct_std_string().size()>(make_ct_std_string()).buffer;
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-16.0.0/bin/../include/c++/v1/__memory/allocator.h:113:38: note: allocation performed here was not deallocated
            return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
 ^
1 error generated.
ASM generation compiler returned: 1
<source>:20:34: error: non-type template argument is not a constant expression
    std::cout << ct_to_rt_string<make_ct_std_string().size()>(make_ct_std_string()).buffer;
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-16.0.0/bin/../include/c++/v1/__memory/allocator.h:113:38: note: allocation performed here was not deallocated
            return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
 ^
1 error generated.
```

`clang++` does accept the code if you change the call to: (Found by Paul Sanders at [Stackoverflow](https://stackoverflow.com/questions/76137985))

```cpp
int main() {
    constexpr size_t N = make_ct_std_string().size();
 std::cout << ct_to_rt_string<N>(make_ct_std_string()).buffer;
}
```

I tested with LLVM 16.0.0:

- https://godbolt.org/z/bdavEGo4x
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVs-Po7gS_mucSykIbKDJIYd00oyeNDN60ozeFRlTAb8xNmub7s4c9m9fGUg66R9z2T3sYSIEpFwuXF99n13cOdlqxC3J7kl2WPHRd8Zua6xH0a1q05y23zvpQDrgUI8tSA0kj4XiuiX0Plx5HMF_wKPz2MCT9F1wULIWl3HgulmMzjdX9qdOig5q4zto5SOCk71U3AJaayz06Bxv0UUkPpB4t9zzeL7EMCwWyqQWamwQCNtz1RorfdcT9vDeuPNW6vZlcLp77AfF_eLQELYjbOfkT6w8fL34Om9H4UH4ypvK-moOBeTufh4HYbTz-MjVax9Ci5ew86zJF5y3hG6uQlzchBlOlZ4m2khUk2NB6IbQPXwFQu8hCa_1eDxiiEHYOQQAkLvDdXrBJDpuz97Z_TlCdrjMC3Mu79N9Tud5sPB68T3_gZUI2TUvGd4mAgAW_Wg1EEqF6QepELzsETw-e0LpzYevC3xTLmmct8j7VwWT2kPPpX7vs1cQjj7EIGz_piBs_1EOUaj7gjV7ILT4yDH4LoC-TeXM0utFf9rvIaERhQUOBz5IS5gGJ4UMVmrvJnm9wSuIyAE-Dyg8Nq8VcSVHWE_5HxaV0XgyKFkTdrgV5ZFL5cCb83Im7Qbk3lPb8pftnRmtwIAM29GYsB1LCdvNig0v2ui1Pw1h2YumuG3HHrUPe4g2HvjMfa59yMeic9Lof0X5gGQPf374O9OzNIMntFxQs2t8HpSxQYTlVId1kkdxFBNa1oGgZRQRWi6UDj5LBWj5mBBaVlWPvbEnQkuulBHcGxuFMiQJC-gWM6gew3PxkEbDgPZobI8NdGgRnvgMboOLDzYvkJ5_iyCd516KSnDnCdtX3wdCdzNWM_BmQBuWARqfCC2qKmh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