Issue 75782
Summary [libcxx] std::normal_distribution may need a static assertion
Labels libc++
Assignees
Reporter GuF-veg
    Consider this code:
![1](https://github.com/llvm/llvm-project/assets/54523006/a7b2e2e9-549c-42b5-98b4-62a4234bcca1)
Using clang + llvm + libc++ toolchain (version: 17.0.6), this code will compile successfully.
But it will lead to runtime errors:
![2](https://github.com/llvm/llvm-project/assets/54523006/58667996-fc88-475b-bd83-a1692b324521)
![3](https://github.com/llvm/llvm-project/assets/54523006/af8d4540-fdca-4615-8e8e-1bd7d702b978)
Refer to cppreference, the template parameter of std::normal_distribution must be a float point type. In another word, it must be float, double or long double, otherwise it will lead to a **Undefined Behaviour**.
I compile this code with GCC-13.2.0, it will arise a compilation error:
![5](https://github.com/llvm/llvm-project/assets/54523006/52e07d42-0223-4abb-8a72-85168c576e14)
The reason is that there is a **static_assert** in GCC STL library random.h:
![6](https://github.com/llvm/llvm-project/assets/54523006/d8428fad-e352-41ff-8e8d-8d7e91870e98)
But there isn't a static_assert in LLVM Libc++ library normal_distribution.h:
![4](https://github.com/llvm/llvm-project/assets/54523006/0b075214-9ebf-492e-8816-c7e888a17877)
So I think it may be a good idea to add a static assertion in LLVM library, in this way we can avoid a lot of latent errors.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to