[更新された要件に応じた質問の更新]
最初に非 null 要素を返すか、例外をスローする次の関数を実装しました。
また、'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;
}