<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/128288>128288</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Readability is too low.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
November20
</td>
</tr>
</table>
<pre>
```c++
template <typename T>
T test(T a){
static_assert(false,"T()");
return a;
}
decltype(test<int>(1)) q1()
{
return 2;
}
int main()
{
q1();
return 0;
}
```
```c++
$clang++ -O0 -fno-elide-constructors -Xclang -ast-print -fsyntax-only decltype.cpp
template <typename T> T test(T a) {
static_assert(false, "T()");
return a;
}
template<> int test<int>(int a)decltype(test<int>(1)) q1() {
return 2;
}
int main() {
q1();
return 0;
}
```
The role of decltype(test<int>(1)) is to deduce the return type of test<int>(1) without actually invoking the test<int> function.
However, due to the presence of static_assert(false, "T()");, the compiler will immediately trigger a compilation error when attempting to instantiate test<int>, even though test<int> is not actually called.
If decltype only needs to deduce the return type and does not need to evaluate the contents of the function body (such as static_assert), then the template function body may not be fully instantiated. In this case, static_assert(false, ...) will not be triggered.
After instantiation, the code does indeed appear somewhat confusing, especially when the template definition, specialization, and decltype usage are mixed together on the same line. This style is not only unaesthetic but may also reduce the code's readability and maintainability.
Problem Analysis
Code Layout Issues:
The template definition, specialization, and decltype usage are compressed into a single line, making the code difficult to read.
This compact writing style can confuse developers during debugging or maintenance.
The Importance of Readability:
Code readability is crucial for team collaboration and long-term maintenance.
A clear code structure helps developers quickly understand the logic and reduces errors.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyslk1v4zYTxz-NfBlYUOg4tg8-eL1P8AQo2mLhQ2_FiBpJ7FKklhzZ6376YijZTtzttkEbCI4hzuvvP5wEYzSNI9pmyw_Z8uMMB2592P7oj9SVFFQxK3113mZPxfjoTH2Qp9gxdb1FJsgWez735LAjOGSL_2XF7gBM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