vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
Classes | Functions
grok.cc File Reference

demonstrates use of vspline::grok_type More...

#include <iostream>
#include <vspline/vspline.h>

Go to the source code of this file.

Classes

struct  grokkee_type< vsize >
 grokkee_type is a vspline::unary_functor returning twice it's input More...
 

Functions

template<typename T >
void twice (const T &in, T &out)
 'regular' function template doing the same More...
 
template<size_t vsize>
void test ()
 test 'groks' a grokkee_type object 'grokkee' as a vspline::grok_type and calls the resulting object, which behaves just as a grokkee_type. More...
 
int main (int argc, char *argv[])
 

Detailed Description

demonstrates use of vspline::grok_type

'grokking' functors is an extremely useful concept. It's applying 'type erasure' to functors, so that the resulting functor - the 'grokked' functor - behaves just like the 'grokkee' but hides the 'grokkee's' type. So what's the point? In functional composition, you build functors by combining several sub-functors. With rising complexity of the costruct, the type becomes quite unmamageably verbose, and every composite functor is unique, so it has to be handled as a template argument. On the other hand, all you want from the combined functor is to perform it's function on it's argument(s), no matter what the internal workings are, and you want to be able to 'slot in' different functors. With type erasure, this becomes much more straightforward: after grokking, the resulting functor is of vspline::grok_type, hiding the inner workings, and all grok_type objects with the same argument signature have the same type, so 'slotting in' becomes easy and the types are manageable. Nevertheless, the optimizer can 'look into' a grok_type and optimize the construct as if you'd not grokked it. std::function does just that: the template argument you pass (or which is inferred by ATD) just specifies return type and argument types, and the resulting object hides the inner workings. vspline::grok_type does not just 'grok' a 'normal' function, but instead it 'groks' a vspline::unary_functor, an object with an 'eval' function with overloads for scalar and SIMD arguments. It does that by using two std::functions internally, but most of the time you can remain unaware of how it's done - you simply use the factory function vspline::grok() and store the result in an 'auto' variable.

Definition in file grok.cc.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 118 of file grok.cc.

◆ test()

template<size_t vsize>
void test ( )

test 'groks' a grokkee_type object 'grokkee' as a vspline::grok_type and calls the resulting object, which behaves just as a grokkee_type.

Definition at line 97 of file grok.cc.

◆ twice()

template<typename T >
void twice ( const T &  in,
T &  out 
)

'regular' function template doing the same

Definition at line 86 of file grok.cc.