Short version: I have a template function that is "universal", but I want to force the user to explicitly specify the type of argument that they pass as an argument to this function.
Any ideas?
Long version: sounds like a terrible design, but here is my case, and I canβt think of anything better now.
I am trying to "implement" ::setsockopt in a small socket class (and I do not want to have many functions by taking different arguments and doing the same thing). For instance:
template< typename OPTION_VALUE_TYPE > bool set_option( int level, int option_name, const OPTION_VALUE_TYPE& value ) { return -1 != ::setsockopt( fd_, level, option_name, &value, sizeof( value ) ); }
BUT, this can lead to the following situation: calling set_option with 1 , trying to set the unsigned char parameter will result in an error, since 1 is int . Proper use:
set_option< unsigned char >( level, option, 1 );
a
set_option( level, option, 1 );
will compile fine, but it will be terribly wrong.
source share