In some cases, see one example below, std::is_floating_point
is returning false
for float
.
#include <iostream>
#include <type_traits>
#include <vector>
int main()
{
::std::cout << typeid(decltype(::std::vector< float >()[::std::vector< float >().size()])).name() << ::std::endl;
if (::std::is_floating_point< decltype(::std::vector< float >()[::std::vector< float >().size()]) >::value)
{
::std::cout << "floating point" << ::std::endl;
}
else
{
::std::cout << "not floating point" << ::std::endl;
}
return 0;
}
Output from GCC
f
not floating point
In this example, one can see that typeid
considers ::std::vector< float >()[::std::vector< float >().size()]
as a float
as it returns the correct name. One can also check that typeid(decltype(::std::vector< float >()[::std::vector< float >().size()])) == typeid(flat)
returns true
. However, std::is_floating_point
is returning false. Why? Is that a bug from C++?
FYI, I checked with both GCC and VisualStudio. In this example, I used std::vector, but one can also try with other libraries, such as Eigen.
decltype
is a compile-time feature, asstd::vector< float >()
creates an empty vector where even index0
is out of bounds and otherwise lead to undefined behavior. – Kelseykelsideclval
to be really safe. Doesn't even require a default ctor. – Fabriannastd::istream
). – Lait