GCC принимает этот код, Clang и MSVC отклоняют его из-за неудачного статического утверждения в утверждении.
Что говорит стандарт?
https:// godbolt.org/z/PKMKzYGsc
template
constexpr int assert() {
static_assert(sizeof(T) == 1);
return 1;
}
template
using Int = int;
template requires (sizeof(T) == 1)
constexpr auto foo(T) -> Int {
return 1;
}
template requires (sizeof(T) > 1)
constexpr auto foo(T a) -> Int {
return 2;
}
static_assert(foo('1') == 1);
static_assert(foo(2) == 2);
Вывод Clang:
:3:19: error: static assertion failed due to requirement 'sizeof(int) == 1'
3 | static_assert(sizeof(T) == 1);
| ^~~~~~~~~~~~~~
:11:30: note: in instantiation of function template specialization 'assert' requested here
11 | constexpr auto foo(T) -> Int {
| ^
:21:15: note: while substituting deduced template arguments into function template 'foo' [with T = int]
21 | static_assert(foo(2) == 2);
| ^
:3:29: note: expression evaluates to '4 == 1'
3 | static_assert(sizeof(T) == 1);
Подробнее здесь: https://stackoverflow.com/questions/790 ... -satisfied
Статическое утверждение срабатывает из возвращаемого типа, когда ведущее ограничение не удовлетворено ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Статическое утверждение, если значение постоянно, в противном случае не [дубликат]
Anonymous » » в форуме C++ - 0 Ответы
- 87 Просмотры
-
Последнее сообщение Anonymous
-