LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc_meta.h File Reference
#include <type_traits>
#include <utility>
#include "swoc/swoc_version.h"
Include dependency graph for swoc_meta.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  swoc::meta::CaseTag< N >
 Case hierarchy. More...
 
struct  swoc::meta::CaseTag< 0 >
 Case hierarchy anchor. More...
 
struct  swoc::meta::vary< Args >
 
struct  swoc::meta::type_list< Types >
 
struct  swoc::meta::let< T >
 

Namespaces

namespace  swoc
 For template deduction guides.
 

Functions

template<typename T>
swoc::meta::TypeFunc ()
 
template<typename T, typename U>
constexpr auto swoc::meta::eraser (U &&u) -> U
 
template<typename... Args>
 swoc::meta::vary (Args...) -> vary< Args... >
 Template argument deduction guide (C++17 required).
 

Detailed Description

Meta programming support utilities.

Definition in file swoc_meta.h.

Function Documentation

◆ eraser()

template<typename T, typename U>
auto swoc::meta::eraser ( U && u) -> U
constexpr

Template parameter eraser.

Template Parameters
TParameter to erase (pass explicitly)
UParameter for forwarded value (implicit)
Parameters
uForward value
Returns
u

This has no effect on u but fools the compiler in to thinking T has been used. This avoids metaprogramming issues with unused template parameters.

Suppose an API has changed a function from "cheer_for_delain" to "cheer_delain". To handle this the metacase support is used, but there is no apparent template parameter. A fake one can be used and defaulted to void. But that creates the unused template parameter warning. This is fixed by doing something to erase the template parameter V while forwarding parameter x. The result is a function f that calls the correct function automatically. Note this can't be in the body of the function because even for SFINAE the function body must compile and the variant with the wrong function will fail.

template <typename V = void>
auto f(UDT x, swoc::meta::CaseTag<0>) -> decltype(cheer_for_delain(eraser<V>(x)))
{ return cheer_for_delain(eraser<V>(x)); }
template <typename V = void>
auto f(UDT x, swoc::meta::CaseTag<1>) -> decltype(cheer_delain(eraser<V>(x)))
{ return cheer_delain(eraser<V>(x)); }
f(x, swoc::meta::CaseArg); // Invoke the correctly named function
Case hierarchy.
Definition swoc_meta.h:66

Definition at line 148 of file swoc_meta.h.

◆ TypeFunc()

template<typename T>
T swoc::meta::TypeFunc ( )

A typed function for use in decltype.

Template Parameters
TThe desired type.
Returns
T

This function has no implementation. It should be used only inside decltype when a specific type (rather than the type of an expression) is needed. For a type T that has a expression default constructor this can be used.

decltype(T())

But if there is no default constructor this will not compile. This is a work around, so the previous expression would be

decltype(meta::TypeFunc<T>())

Note this can also be a problem for even built in types like unsigned long for which the expression

decltype(unsigned long())

does not compile.