[question update according to updated requirements]
I’ve implemented following function which should return either first not null element or throw an exception.
Also could you invent more classic and shorter name like ‘max’, ‘min’, ‘pair’?
template <typename T> T select_first_not_empty( const T& a, const T&b ) { static T null = T(); if ( a == null && b == null ) throw std::runtime_error( 'null' ); return a != null ? a : b; } int main() { const int a1 = 2; const int b1 = 0; const int* a2 = 0; const int* b2 = new int(5); const std::string a3 = ''; const std::string b3 = ''; std::cout << select_first_not_empty( a1, b1 ) << std::endl; std::cout << select_first_not_empty( a2, b2 ) << std::endl; std::cout << select_first_not_empty( a3, b3 ) << std::endl; return 0; }
you can try do next