I'm trying to create a template where a default value can be passed as a non-type parameter.
The original type (WINAPI's HANDLE
) is a pointer type from the compiler perspective, but otherwise is treated as an integral type from a user perspective.
// Somewhere in system headers
#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)
typedef void *HANDLE;
// My code
template<typename Handle, Handle Default>
class HandleWrapper
{
...
};
HandleWrapper<HANDLE, INVALID_HANDLE_VALUE>; // error: invalid nontype template argument of type `HANDLE`
My current workaround:
template<typename Handle, uintptr_t Default>
class HandleWrapper
{
static_assert(std::is_pointer<Handle>::value, "Handle must be a pointer");
static constexpr Handle DefaultHandle = reinterpret_cast<Handle>(Invalid);
};
I guess the right solution is to somehow specify that typename Handle
should be treated as an integral type (uintptr_t
) as long as conversation is not narrowing.