Код: Выделить всё
class ConstevalConstructible {
public:
consteval ConstevalConstructible(int value) : value_(value) {
// Validate value and fail compile if invalid.
}
// Copyable, movable, et cetera
private:
int value_;
}
Код: Выделить всё
template
struct Wrapper {
template
constexpr Wrapper(Args&&... args) : inner(std::forward(args)...) {}
T inner;
};
Код: Выделить всё
// Fine
Wrapper foo{runtime_value};
// Also fine: Wrapper's constexpr constructor is evaluated at compile
// time and thus the ConstexprConstructible's constructor is too.
constexpr Wrapper bar{42};
Код: Выделить всё
// Fails to compile: the constexpr Wrapper constructor is not an
// immediate function, so it cannot invoke ConstevalConstructible's
// constructor using arguments that are not (within its body)
// constexpr.
Wrapper foo{5};
// Doesn't fix the problem from the compiler's point of view.
constinit Wrapper foo{5};
// This does work as long as ConstevalConstructible is movable or
// copyable, but gets annoying.
Wrapper bar{ConstevalConstructible{5}};
Код: Выделить всё
ConstevalConstructibleКонструктор Это похоже на то, как работает noException, где возможность выдачи метода может быть указана в терминах содержащегося типа. Однако мне неизвестен аналогичный способ сделать конструктор или функцию условно константными. Я подумывал о добавлении перегрузки consteval, но не знаю, как ее ограничить:
Код: Выделить всё
template
struct Wrapper {
template
constexpr Wrapper(Args&&... args) : inner(std::forward(args)...) {}
template
consteval Wrapper(Args&&... args)
requires requires { /** What would I put here? **/ }
: inner(std::forward(args)...) {}
T inner;
};
Подробнее здесь: https://stackoverflow.com/questions/790 ... forwarding
Мобильная версия