Skip to content

Commit

Permalink
algo: add coalesce
Browse files Browse the repository at this point in the history
I quite like this little thing even though I don't use it much anymore
(mostly because I don't use pointers much).
  • Loading branch information
Hedede committed Mar 11, 2024
1 parent 4b203b5 commit ba7a0e3
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions algorithm/include/aw/algorithm/coalesce.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef aw_algorithm_coalesce_h
#define aw_algorithm_coalesce_h

namespace aw {
/*!
* Returns first non-null pointer, similar to
* the COALESCE function in SQL.
*/
template <typename T, typename... Ts>
T* coalesce(T* first, Ts*... args)
{
if constexpr (sizeof...(Ts) > 0)
return first ? first : coalesce(args...);
return first;
}
} // namespace aw

#endif // aw_algorithm_coalesce_h

0 comments on commit ba7a0e3

Please sign in to comment.