Код: Выделить всё
// Changing this to constexpr will cause the function
// to be invoked at runtime.
consteval int abs(int x) {
return x < 0 ? -x : x;
}
template
struct Test {
bool isValid(int y) {
return y < abs(N); // Error.
}
};
Вызов константной функции «abs» не является постоянным выражением... подвыражение недопустимо в постоянном выражении [ valid_consteval_call]
(проверено в clang 17.0.6). Эта ошибка по-прежнему возникает, если вы пытаетесь определить статическую переменную constexpr на основе N:
Код: Выделить всё
// Changing this to constexpr works though!
consteval int abs(int x) {
return x < 0 ? -x : x;
}
template
struct Test {
static constexpr int ABS_N = abs(N); // Error
bool isValid(int y) {
return y < ABS_N;
}
};
Код: Выделить всё
template
consteval int abs() {
return X < 0 ? -X : X;
}
template
struct Test {
bool isValid(int y) {
return y < abs();
}
}
Код: Выделить всё
struct Test {
static constexpr int N = 7;
bool isValid(int y) {
return y < abs(N);
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... -consteval