scl-utility

overload_cast

Utility for disambiguating overloaded functions and member functions by argument types.

Contents:


overload_cast<Args...>

Selects a specific overload of a function or member function pointer based on argument types.

Semantics

Motivation

Taking the address of an overloaded function normally requires a verbose static_cast to the exact function pointer type. overload_cast provides a concise, readable alternative:

// Without overload_cast — verbose and error-prone
auto p = static_cast<void (Widget::*)(int)>(&Widget::update);

// With overload_cast — concise and clear
auto p = scl::overload_cast<int>(&Widget::update);

Examples

#include <scl/utility/type_traits/overload_cast.h>

struct Widget {
    void update(int);
    void update(double) const;
    void update(int, int) noexcept;
};

void process(int);
void process(double) noexcept;

// Member function overloads
auto p1 = scl::overload_cast<int>(&Widget::update);          // void(int)
auto p2 = scl::overload_cast<double>(&Widget::update);       // void(double) const
auto p3 = scl::overload_cast<int, int>(&Widget::update);     // void(int, int) noexcept

// Free function overloads
auto f1 = scl::overload_cast<int>(&process);                 // void(int)
auto f2 = scl::overload_cast<double>(&process);              // void(double) noexcept

Typical Use Cases