Template Function Acts::template_switch

Function Documentation

template<template<size_t> class Callable, size_t N, size_t NMAX, typename ...Args>
decltype(Callable<N>::invoke(std::declval<Args>()...)) Acts::template_switch(size_t v, Args&&... args)

Dispatch a call based on a runtime value on a function taking the value at compile time.

This function allows to write a templated functor, which accepts a size_t like paramater at compile time. It is then possible to make a call to the corresponding instance of the functor based on a runtime value. To achieve this, the function essentially created a if cascade between N and NMAX, attempting to find the right instance. Because the cascade is visible to the compiler entirely, it should be able to optimize.

Note

Callable is expected to have a static member function invoke that is callable with Args

Template Parameters
  • Callable: Type which takes a size_t as a compile time param

  • N: Value from which to start the dispatch chain, i.e. 0 in most cases

  • NMAX: Maximum value up to which to attempt a dispatch

Parameters
  • v: The runtime value to dispatch on

  • args: Additional arguments passed to Callable::invoke().