Code: Select all
template std::string_view type_str() { /* returns the name of the type */ }
template const auto type_name = type_str();
template void fptr(const T *const t) {
if (t)
std::print("{} = {:X}.\n", static_cast(t), *t);
else
std::print("nullptr.\n");
}
// #1
template requires std::is_pointer_v void f(T t) {
std::print("[{}] is pointer --> ", type_name);
fptr(t);
}
// #2
template void f(T t) {
std::print("[{}] is NOT pointer.\n", type_name);
}
int main()
{
const auto v0 = 0xfabada;
auto v1 = 0xc0ffee;
f(&v0);
f(&v1);
f(nullptr);
return 0;
}
Code: Select all
[const int*] is pointer --> 0xWHATEVER = FABADA.
[int*] is pointer --> 0xWHATEVER = C0FFEE.
[std::nullptr_t] is NOT pointer.
Code: Select all
template
constexpr T *NULLPTR = nullptr;
…
f(&v0);
f(&v1);
f(nullptr);
f(NULLPTR);
…
Code: Select all
[const int*] is pointer --> 0xWHATEVER = FABADA.
[int*] is pointer --> 0xWHATEVER = C0FFEE.
[std::nullptr_t] is NOT pointer.
[int*] is pointer --> nullptr.
Beispielcode hier.