Is there a way to limit a template parameter T to a specific type or category?
The below code works but I want to make it simpler:
#include <iostream>
#include <type_traits>
template <typename T>
constexpr auto func( const T num ) -> T
{
static_assert( std::is_floating_point_v<T>, "Floating point required." );
return num * 123;
}
int main( )
{
std::cout << func( 4345.9 ) << ' ' // should be ok
<< func( 3 ) << ' ' // should not compile
<< func( 55.0f ) << '\n'; // should be ok
}
I want to get rid of the static_assert and write something like this:
template < std::is_floating_point_v<T> >
constexpr auto func( const T num ) -> T
{
return num * 123;
}
Any suggestions? Anything from type_traits or maybe concepts would be better.
floatanddouble:)