static assertion fires from return type when leading constraint is not satisfied

preview_player
Показать описание

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!static assertion fires from return type when leading constraint is not satisfied

GCC accepts this code, Clang and MSVC reject it due to failed static assertion in assert.
What does the standard say?
assert
template typename T
constexpr int assert() {
static_assert(sizeof(T) == 1);
return 1;
}

template auto I
using Int = int;

template typename T requires (sizeof(T) == 1)
constexpr auto foo(T) - Int assert T () {
return 1;
}

template typename T requires (sizeof(T) 1)
constexpr auto foo(T a) - Int 1 {
return 2;
}

static_assert(foo('1') == 1);
static_assert(foo(2) == 2);

template typename T
constexpr int assert() {
static_assert(sizeof(T) == 1);
return 1;
}

template auto I
using Int = int;

template typename T requires (sizeof(T) == 1)
constexpr auto foo(T) - Int assert T () {
return 1;
}

template typename T requires (sizeof(T) 1)
constexpr auto foo(T a) - Int 1 {
return 2;
}

static_assert(foo('1') == 1);
static_assert(foo(2) == 2);

Clang output:
source :3:19: error: static assertion failed due to requirement 'sizeof(int) == 1'
3 | static_assert(sizeof(T) == 1);
| ^~~~~~~~~~~~~~
source :11:30: note: in instantiation of function template specialization 'assert int ' requested here
11 | constexpr auto foo(T) - Int assert T () {
| ^
source :21:15: note: while substituting deduced template arguments into function template 'foo' [with T = int]
21 | static_assert(foo(2) == 2);
| ^
source :3:29: note: expression evaluates to '4 == 1'
3 | static_assert(sizeof(T) == 1);

source :3:19: error: static assertion failed due to requirement 'sizeof(int) == 1'
3 | static_assert(sizeof(T) == 1);
| ^~~~~~~~~~~~~~
source :11:30: note: in instantiation of function template specialization 'assert int ' requested here
11 | constexpr auto foo(T) - Int assert T () {
| ^
source :21:15: note: while substituting deduced template arguments into function template 'foo' [with T = int]
21 | static_assert(foo(2) == 2);
| ^
source :3:29: note: expression evaluates to '4 == 1'
3 | static_assert(sizeof(T) == 1);

Tags: c++,language-lawyer,c++20,c++-conceptsSource of the question:

Question and source license information:
Рекомендации по теме
visit shbcf.ru