llvm / llvm/llvm-project

-Wconstant-conversion missing out-of-range integral constant in NTTP and template initialization

Open
#181,747 0 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**Description**

`-Wconstant-conversion` does not emit a diagnostic when an integer constant outside the representable range of an integral is used as a non-type template parameter (NTTP) value.

This includes both default template arguments and explicit template arguments.

Although the program is well-formed C++ (the value is reduced modulo the destination type), the same conversion *does* trigger a diagnostic in normal variable initialization. This suggests inconsistent diagnostic handling for identical constant conversions.

**1. Default non-type template parameter**

```cpp
#include

template < uint8_t N = 400 >
struct TOk {};
```

**Actual behavior**: No diagnostic is emitted with ```-Wconstant-conversion```

**Expected behavior**

A diagnostic indicating that the constant value cannot be represented in `uint8_t` (even though the program remains well-formed and the value is reduced modulo the type).

**2. Explicit template argument**
Similar case of lacking diagnostic emission (less obvious detection)

```cpp
template
struct S {};

S<400> s; // no warning
```

**3. Control example — equivalent initialization does warn**

```cpp
unsigned char x = 400; // warning emitted here
```

This shows the same constant conversion is diagnosed in a normal initialization context but not when performed as part of template argument checking.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.