llvm / llvm/llvm-project

C++23: template function parameter instantiation in none-invoked function

Open
#202,318 3 comments 5 reactions 0 assignees View on GitHub
clang:frontend concepts diverges-from:gcc diverges-from:msvc
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```c++
#include
#include

template
constexpr auto foo(std::uniform_real_distribution x) {
}

template
constexpr auto foo(std::uniform_int_distribution c) {
}

auto main() -> int {
foo({});
}
```
Expected behavior is that it compiles successfully with clang-22 and flags `-std=c++23 -stdlib=libc++`.
However it fails with:
```
/cefs/26/26ccf7a2fd1f9d4667de2307_consolidated/compilers_c++_clang_22.1.0/bin/../include/c++/v1/__random/uniform_real_distribution.h:29:17: error: static assertion failed due to requirement 'std::__libcpp_random_is_valid_realtype::value': RealType must be a supported floating-point type
29 | static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:14:14: note: in instantiation of template class 'std::uniform_real_distribution' requested here
14 | foo({});
| ^
:14:5: note: while substituting deduced template arguments into function template 'foo' [with T = int]
14 | foo({});
| ^
```

It also fails when using gcc's stdlib.
https://godbolt.org/z/b6ccGPcj5 : notice that it compiles successfully with gcc and msvc

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the C++23 example with clang-22, libc++, and the flags given in the issue, then inspect __random/uniform_real_distribution.h at the reported static assertion. Compare the libc++ behavior with GCC's standard library and MSVC using the linked Compiler Explorer example. Done means the example compiles successfully without instantiating the invalid distribution diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.