#include <type_traits>
#include <utility>
template <typename T>
struct Foo {
template <typename U>
T ValueOr(U&&) {
static_assert(sizeof(U) > 1);
return {};
}
};
template <typename T>
struct ForwardedDeclval {
T operator()();
};
template <typename... Ts, typename F>
consteval void InstantiateForStaticAssertions(F f) {
static_assert(std::is_same_v<decltype(f(ForwardedDeclval<Ts>()...)), void>);
}
template <typename T>
struct FooWrapper {
template <typename U>
T ValueOr(U&& u) {
InstantiateForStaticAssertions<Foo<T>, decltype(u)>(
[](auto t, auto u) { t().ValueOr(u()); });
return {};
}
};
int main() {
FooWrapper<int> f;
f.ValueOr(1);
//f.ValueOr('5');
}
Dec 7, 2023
[C++] ValueOr(type) type check.
https://godbolt.org/z/qYG6rsY7h
Labels:
code,
cpp,
cpp_sample,
cpp20,
sample_code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.