If T cannot be compared to nullptr, the whole thing is ill-formed.
https://godbolt.org/z/eqehez
#include <type_traits>
template<typename T>
void foo()
{
static_assert(
std::is_convertible<decltype(std::declval<T>() != nullptr), bool>::value,
"T cannot be compared to nullptr.");
}
int main()
{
foo<int>();
}
This results in a compiler diagnostic, and not an assertion, using Microsoft's own compiler:
example.cpp
<source>(7): error C2446: '!=': no conversion from 'nullptr' to 'T'
with
[
T=int
]
<source>(7): note: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type
<source>(13): note: see reference to function template instantiation 'void foo<int>(void)' being compiled
<source>(7): error C2955: 'std::is_convertible': use of class template requires template argument list
C:/data/msvc/14.28.29333/include\type_traits(354): note: see declaration of 'std::is_convertible'
<source>(7): error C2057: expected constant expression
Compiler returned: 2
It doesn't matter whether you use != or == here. Whether you can use is_same is a moot point. This seems to answer the questions you asked. The overall purpose of this static_assert is somewhat unclear. I had a fleeting thought that this might occasionally encounter some evil T that overloads == and != operators for comparing T with std::nullptr_t, which then returns something other than bool. This is a bit of a stretch, but is possible. I looked at that github repo, and that didn't appear to be the case. I think this test is broken.
Treally cannot be compared tonullptr, thatstd::declval<T>() != nullptrwould be a diagnosable error.