Building a call table to template functions in C++ - c++

I have a template function where the template parameter is an integer. In my program I need to call the function with a small integer that is determined at run time. By hand I can make a table, for example:
void (*f_table[3])(void) = {f<0>,f<1>,f<2>};
and call my function with
f_table[i]();
Now, the question is if there is some automatic way to build this table to arbitrary order. The best I can come up with is to use a macro
#define TEMPLATE_TAB(n) {n<0>,n<1>,n<2>}
which at leasts avoids repeating the function name over and over (my real functions have longer names than "f"). However, the maximum allowed order is still hard coded. Ideally the table size should only be determined by a single parameter in the code. Would it be possible to solve this problem using templates?

It can be done by 'recursive' dispatching: a template function can check if it's runtime argument matches it's template argument, and return the target function with the template argument.
#include <iostream>
template< int i > int tdispatch() { return i; }
// metaprogramming to generate runtime dispatcher of
// required size:
template< int i > int r_dispatch( int ai ) {
if( ai == i ) {
return tdispatch< i > ();
} else {
return r_dispatch< i-1 >( ai );
}
}
template<> int r_dispatch<-1>( int ){ return -1; }
// non-metaprogramming wrapper
int dispatch( int i ) { return r_dispatch<100>(i); }
int main() {
std::cout << dispatch( 10 );
return 0;
}

You can create a template that initializes a lookup table by using recursion; then you can call the i-th function by looking up the function in the table:
#include <iostream>
// recursive template function to fill up dispatch table
template< int i > bool dispatch_init( fpointer* pTable ) {
pTable[ i ] = &function<i>;
return dispatch_init< i - 1 >( pTable );
}
// edge case of recursion
template<> bool dispatch_init<-1>() { return true; }
// call the recursive function
const bool initialized = dispatch_init< _countof(ftable) >( ftable );
// the template function to be dispatched
template< int i > void function() { std::cout << i; }
// dispatch functionality: a table and a function
typedef void (*fpointer)();
fpointer ftable[100];
void dispatch( int i ){ return (ftable[i])(); }
int main() {
dispatch( 10 );
}

[Proven wrong: I don't think that can be done purely with templates.]
Take a look at the boost preprocessor library.

Following xtofl I decided to go with the following macro/template solution shown below. I needed the macro because I want to build these dispatch tables for many functions, and I cannot see how to do that with one single template function.
#include <iostream>
using namespace std;
#define MAX_ORDER 8
#define DISPATCH_TABLE(table,fpointer,function,N) \
template< int i > fpointer *function##dispatch_init(fpointer function_table[]) \
{ \
function_table[i] = function<i>; \
return function##dispatch_init< i - 1 >(function_table); \
} \
template<> fpointer *function##dispatch_init<-1>(fpointer function_table[]) \
{ \
return function_table; \
} \
const fpointer *table = function##dispatch_init<N>(new fpointer[N])
typedef void (*fpointer)(void);
template<int N>
void printN(void)
{
cout << N << endl;
}
DISPATCH_TABLE(printN_table, fpointer, printN, MAX_ORDER);
int main(void)
{
for (int i = 0; i < MAX_ORDER; i++)
printN_table[i]();
return 0;
}

Related

Value not usable in constant expression alternative pattern

I am looking for an alternative c++ pattern to achieve this:
Read from an option from a file (let's say its either A or B).
In a cycle I want to repeat a call to a template function depending on the option, but I don't want to check the value each time, instead, I want the compiler to generate both possibilities and choose the one with template A if the option is set to A, and with template B if the option is set to B.
If i do this though:
Option option = readFromFile("AorB");
for(int i = 0; i < 100000; ++i)
{
performOperation<option>(); // Long but fast function I don't want to define twice
}
I get the following error:
error: the value of 'option' is not usable in a constant expression
How can I achieve the desired behaviour?
To make the code weirder and more meta ;) you could play a bit with variadic templates, lambdas and constexpr implicit cast:
#include <iostream>
template <char C>
struct Option {
constexpr operator char() {
return C;
}
};
template <char Opt>
void performOperation() {
std::cout << Opt << std::endl;
}
template <char... Options>
void runOption() {
char optionFromFile = 'a';
int dummy[] = {([](auto option, char chosen) {
if (chosen == option) {
for(int i = 0; i < 5; ++i) {
performOperation<option>();
}
}
}(Option<Options>{}, optionFromFile), 0)...};
static_cast<void>(dummy);
}
int main() {
runOption<'a', 'b'>();
}
[live demo]
Have fun!
As others have mentioned, you can't pass a variable to something that expects a compile time constant.
If you've got something that is either "A" or "B" and you're worried about checking for that each time then you could expand the loop/condition yourself:
Option option = readFromFile("AorB");
if(option.isA())
{
for(int i = 0; i < 100000; ++i)
{
performOperationA();
}
else
{
for(int i = 0; i < 100000; ++i)
{
performOperationB();
}
}
If you want the operation to select the behavior at runtime, then you may want to create a class to encapsulate the behavior that varies. Next create an instance of the class once based on the option value A or B. Then inside the loop, you pass the class instance to operation.
I've provided an example below that implements OptionA and OptionB in a class hierarchy. If you do it this way, then you don't even need a template at all. But you didn't provide much detail on how the behavior of your operation varies, so I didn't want to assume too much about what you have to work with. The template is only required if you have two unrelated classes that implement an identical interface.
#include <iostream>
#include <string>
class OptionType {
public: virtual int calculate( int x ) = 0;
};
class OptionA :public OptionType {
public: int calculate( int x ) { return x+99; }
};
class OptionB : public OptionType {
public: int calculate( int x ) { return x*100; }
};
template<class T>
void performOperation( T& option, int x ) {
// your performOperation is a long function
// this one is short but shows how the behavior can vary by option
std::cout << option.calculate( x ) << std::endl;
}
int main( int argc, char* argv[] )
{
// Option option = readFromFile("AorB");
// pass A or B from the command line
char option = (argc > 1) ? argv[1][0] : 'A'; // your code reads this from a file
OptionType* optionObject;
if( option == 'A' ) optionObject = new OptionA();
else optionObject = new OptionB();
for(int i = 0; i < 10; ++i)
{
performOperation( *optionObject, i );
}
}
you can't because the option is a variable, and a template is compile time. the performOperation needs to receive a constant value on the <>.
and since the operations occours on runtime - you need an if.
But you could use the branch prediction to do less work - if you sort the vector before passing to for / if call(value) else call_2(value), it would run much faster.
Template arguments must be known at compile time. At run time, it's too late to instantiate new instances of a template. You will need to force the generation of each instance that may be required at run time and implement some form of dispatching. The simplest approach is to have a dispatching function with a switch. For example :
enum class Option {
Opt1,
Opt2,
Opt3
};
template<Option opt>
void Operation() {}
void performOperation(const Option opt)
{
switch (opt)
{
case(Option::Opt1):
Operation<Option::Opt1>();
break;
case(Option::Opt2):
Operation<Option::Opt2>();
break;
case(Option::Opt3):
Operation<Option::Opt3>();
break;
default:
// Handle however you want
}
}
Another solution would use a map of std::function :
#include <map>
#include <functional>
enum class Option {
Opt1,
Opt2,
Opt3
};
template<Option opt>
void Operation() {}
const std::map<Option, std::function<void()>> func_map = {
{ Option::Opt1, []{Operation<Option::Opt1>(); }},
{ Option::Opt2, []{Operation<Option::Opt2>(); }},
{ Option::Opt3, []{Operation<Option::Opt3>(); }}
};
void performOperation(const Option opt)
{
func_map.at(opt)();
}
If I understood the question correctly.
You could try this way:
for(int i = 0; i < 100000; ++i)
{
#ifdef A
read("A");
#endif
#ifdef B
read("B");
#endif
}
and at compiler level you can choose:
g++ main.cpp -D A
or
g++ main.cpp -D B

Execute an instruction later

I would like to give an instruction as a parameter:
execute_at_frame(int frame_number, <instruction>)
{
for(f = 1 ; f < F_MAX ; f++)
{
/* other instructions */
if (f == frame_number)
/* execute <instruction> */
/* other instructions */
}
}
One type of call: execute_at_frame(5,execute(42));
Another type of call: execute_at_frame(6,process());
Is that (or something similar) possible?
Thanks in advance :-)
Yes, if you use std::bind (C++11):
template <class F>
void execute_at_frame(int frame_number, F instruction)
{
for(int f = 1 ; f < F_MAX ; f++)
{
/* other instructions */
if (f == frame_number)
instruction();
/* other instructions */
}
}
/* ... */
execute_at_frame(5,process); // no bind for functions without parameters
execute_at_frame(5,std::bind(execute,42));
Otherwise you'll have to prepare a interface for instructions.
Your <instruction> parameter can either be a function pointer (i.e. a pointer to an execute function); or, it can be a reference to an instance of a class, which has an execute method.
You can pass a function pointer along with (if needed) some parameters. It could look like this:
typedef void (*Instruction)(int);
void foo(int)
{
// do something
}
void execute_at_frame(int frame_number, Instruction ins, int param)
{
for(int f = 1 ; f < F_MAX ; f++)
{
/* other instructions */
if (f == frame_number)
ins(param);
}
}
Sample usage:
execute_at_frame(1000, foo, 42);
If you use variadic templates, you can make it work with any signature. Simplified example:
void foo(int)
{
}
float bar(int, char, double)
{
return 1.0;
}
template<typename F, typename... Args>
void execute(F ins, Args... params)
{
ins(params...);
}
int main()
{
execute(foo, 1);
execute(bar, 1, 'a', 42.0);
}
You'll need C++11 compiler for that.
your parameter also can be a base class pointer,point to Derived class which has a virtual function
Code for using a function as a parameter:
#include <functional>
#include <iostream>
using namespace std;
int instruction(int instruc)
{
return instruc ;
}
template<typename F>
void execute_at_frame(int frame, const F& function_instruction)
{
std::cout << function_instruction(frame) << '\n';
}
int main()
{
execute_at_frame(20, instruction); // use reference
execute_at_frame(40, &instruction); // use pointer
cout<<" \nPress any key to continue\n";
cin.ignore();
cin.get();
return 0;
}

Generic declaration of member variables

is there any way to declare a variety number of member variables from different user-data type generically using template operator?
consider this code:
class a {
int member;
void ProcessMemberVariable ();
};
class b {
char member;
void ProcessMemberVariable ();
};
... // arbitrary number of such classes
class test {
template <typename T>
void declare (T a ) {
// each time this member function is called a new member variable of the
// user data type T shall be declared in the instance of the class test??
}
};
int ()
{
test Test;
Test.template declare<a>(a A);
Test.template declare<b>(b B);
...
}
Imagine You want to implement an interface which is apple to set any kind of user defined data type. Since I know the identifier of user-defined data type only when I declare an instance of class "test" and call its member function...
I appreciate each suggestion..
What you are describing sounds like dynamically adding members to an object, and this isn't possible in C++. There are various ways to get a similar effect in certain situations, but you would need to describe a situation where you thought this would be useful.
As stated there is no way to dynamically add member variables at runtime.
However, if you know the list of types that you may want to add at runtime you could achieve this behaviour using boost::variant. Below is a trivial example (
#include <iostream>
#include <string>
#include <map>
#include <boost/variant.hpp>
using namespace std;
class Test
{
public:
typedef boost::variant< long, double, string > VariantType;
template< typename T >
void Declare( std::string name, T val )
{
VariantType newVal = val;
varMap.insert( std::make_pair( std::move( name ), std::move( val ) ) );
}
VariantType Get( const std::string& name )
{
return varMap[ name ];
}
template< typename T >
T GetValue( const std::string& name )
{
return boost::get<T>( varMap[name] );
}
private:
std::map< string, VariantType > varMap;
};
int main()
{
Test t{};
t.Declare( "Var1", 10l );
t.Declare( "pi", 3.14159);
t.Declare( "AString", "SomeName" );
cout << "t.get( Var1 ) " << t.GetValue<long>( "Var1" ) << "\n";
cout << "t.get( pi ) " << t.GetValue<double>( "pi" ) << "\n";
cout << "t.get( AString ) " << t.GetValue<string>( "AString" ) << "\n";
return 0;
}
See: http://www.boost.org/doc/libs/1_49_0/doc/html/variant.html for details on how to use boost::variant.

How to call a templated operator overload without using the word 'operator'?

class RunAround;
class HopUpAndDown;
class Sleep;
template<typename Acts> int doThis();
template<> int doThis<RunAround>() { /* run run run.. */ return 3; }
template<> int doThis<HopUpAndDown>() { /* hop hop hop.. */ return 2; }
template<> int doThis<Sleep>() { /* zzz.. */ return -2; }
struct Results
{
template<typename Act> int& operator()()
{
static int result;
return result;
}
};
int main()
{
Results results;
//results<RunAround>() = doThis<RunAround>();
results.operator ()<RunAround>() = doThis<RunAround>();
results.operator ()<Sleep>() = doThis<Sleep>();
return 0;
};
If I remove the comment, the compiler thinks I am calling operator() in non-existant template class Results<RunAround> when I want operator<RunAround>() in class Results.
If I want to continue using an operator overload instead of a normal name, am I doomed to use the awful syntax below the comment (which does work)?
The most comfortable thing is to let template argument deduction work for you:
struct Results {
template<typename Act> int& operator()(Act) { /* ... */ }
};
results(RunAround()) = /* ... */;

C++: Function wrapper that behaves just like the function itself

How can I write a wrapper that can wrap any function and can be called just like the function itself?
The reason I need this: I want a Timer object that can wrap a function and behave just like the function itself, plus it logs the accumulated time of all its calls.
The scenario would look like this:
// a function whose runtime should be logged
double foo(int x) {
// do something that takes some time ...
}
Timer timed_foo(&foo); // timed_foo is a wrapping fct obj
double a = timed_foo(3);
double b = timed_foo(2);
double c = timed_foo(5);
std::cout << "Elapsed: " << timed_foo.GetElapsedTime();
How can I write this Timer class?
I am trying something like this:
#include <tr1/functional>
using std::tr1::function;
template<class Function>
class Timer {
public:
Timer(Function& fct)
: fct_(fct) {}
??? operator()(???){
// call the fct_,
// measure runtime and add to elapsed_time_
}
long GetElapsedTime() { return elapsed_time_; }
private:
Function& fct_;
long elapsed_time_;
};
int main(int argc, char** argv){
typedef function<double(int)> MyFct;
MyFct fct = &foo;
Timer<MyFct> timed_foo(fct);
double a = timed_foo(3);
double b = timed_foo(2);
double c = timed_foo(5);
std::cout << "Elapsed: " << timed_foo.GetElapsedTime();
}
(BTW, I know of gprof and other tools for profiling runtime, but having such a Timer object to log the runtime of a few selected functions is more convenient for my purposes.)
Basically, what you want to do is impossible in current C++. For any number of arity of function you want to wrap, you need to overload by
const reference
non-const reference
But then it's still not perfectly forwarding (some edge cases still stand), but it should work reasonable well. If you limit yourself to const references, you can go with this one (not tested):
template<class Function>
class Timer {
typedef typename boost::function_types
::result_type<Function>::type return_type;
public:
Timer(Function fct)
: fct_(fct) {}
// macro generating one overload
#define FN(Z, N, D) \
BOOST_PP_EXPR_IF(N, template<BOOST_PP_ENUM_PARAMS(N, typename T)>) \
return_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(N, T, const& t)) { \
/* some stuff here */ \
fct_(ENUM_PARAMS(N, t)); \
}
// generate overloads for up to 10 parameters
BOOST_PP_REPEAT(10, FN, ~)
#undef FN
long GetElapsedTime() { return elapsed_time_; }
private:
// void() -> void(*)()
typename boost::decay<Function>::type fct_;
long elapsed_time_;
};
Note that for the return type, you can use boost's function types library. Then
Timer<void(int)> t(&foo);
t(10);
You can also overload using pure value parameters, and then if you want to pass something by reference, use boost::ref. That's actually a pretty common technique, especially when such parameters are going to be saved (this technique is also used for boost::bind):
// if you want to have reference parameters:
void bar(int &i) { i = 10; }
Timer<void(int&)> f(&bar);
int a;
f(boost::ref(a));
assert(a == 10);
Or you can go and add those overloads for both const and non-const versions as explained above. Look into Boost.Preprocessor for how to write the proper macros.
You should be aware that the whole thing will become more difficult if you want to be able to pass arbitrary callables (not only functions), since you will need a way then to get their result type (that's not all that easy). C++1x will make this sort of stuff way easier.
Here is an easy way to wrap functions.
template<typename T>
class Functor {
T f;
public:
Functor(T t){
f = t;
}
T& operator()(){
return f;
}
};
int add(int a, int b)
{
return a+b;
}
void testing()
{
Functor<int (*)(int, int)> f(add);
cout << f()(2,3);
}
I assume you need this for test purpose and aren't going to use them as a real proxies or decorators. So you won't need to use operator() and can use any other more-less convenient method of call.
template <typename TFunction>
class TimerWrapper
{
public:
TimerWrapper(TFunction function, clock_t& elapsedTime):
call(function),
startTime_(::clock()),
elapsedTime_(elapsedTime)
{
}
~TimerWrapper()
{
const clock_t endTime_ = ::clock();
const clock_t diff = (endTime_ - startTime_);
elapsedTime_ += diff;
}
TFunction call;
private:
const clock_t startTime_;
clock_t& elapsedTime_;
};
template <typename TFunction>
TimerWrapper<TFunction> test_time(TFunction function, clock_t& elapsedTime)
{
return TimerWrapper<TFunction>(function, elapsedTime);
}
So to test some of yours function you should use only test_time function and not the direct TimerWrapper structure
int test1()
{
std::cout << "test1\n";
return 0;
}
void test2(int parameter)
{
std::cout << "test2 with parameter " << parameter << "\n";
}
int main()
{
clock_t elapsedTime = 0;
test_time(test1, elapsedTime).call();
test_time(test2, elapsedTime).call(20);
double result = test_time(sqrt, elapsedTime).call(9.0);
std::cout << "result = " << result << std::endl;
std::cout << elapsedTime << std::endl;
return 0;
}
You may probably find an answer if you look at the implementation of std::tr1::function that you include.
In c++11, std:: function is implemented with variadic templates. Using such templates your timer class can look like
template<typename>
class Timer;
template<typename R, typename... T>
class Timer<R(T...)>
{
typedef R (*function_type)(T...);
function_type function;
public:
Timer(function_type f)
{
function = f;
}
R operator() (T&&... a)
{
// timer starts here
R r = function(std::forward<T>(a)...);
// timer ends here
return r;
}
};
float some_function(int x, double y)
{
return static_cast<float>( static_cast<double>(x) * y );
}
Timer<float(int,double)> timed_function(some_function); // create a timed function
float r = timed_function(3,6.0); // call the timed function
Stroustrup had demonstrated a function wrapper(injaction) skill with overloading the operator->. The key idea is: operator-> will repeatly called until it meets a native pointer type, so let Timer::operator-> return a temp object, and the temp object return its pointer. Then following will happen:
temp obj created (ctor called).
target function called.
temp obj destructed (dtor called).
And you can inject any code within the ctor and the dtor. Like this.
template < class F >
class Holder {
public:
Holder (F v) : f(v) { std::cout << "Start!" << std::endl ; }
~Holder () { std::cout << "Stop!" << std::endl ; }
Holder* operator->() { return this ; }
F f ;
} ;
template < class F >
class Timer {
public:
Timer ( F v ) : f(v) {}
Holder<F> operator->() { Holder<F> h(f) ; return h ; }
F f ;
} ;
int foo ( int a, int b ) { std::cout << "foo()" << std::endl ; }
int main ()
{
Timer<int(*)(int,int)> timer(foo) ;
timer->f(1,2) ;
}
The implementation and the usage are both easy.
A solution using macros and templates: For example you want to wrap
double foo( double i ) { printf("foo %f\n",i); return i; }
double r = WRAP( foo( 10.1 ) );
Before and after calling foo() the wrapper functions beginWrap() and endWrap() should be called. (With endWrap() being a template function.)
void beginWrap() { printf("beginWrap()\n"); }
template <class T> T endWrap(const T& t) { printf("endWrap()\n"); return t; }
The macro
#define WRAP(f) endWrap( (beginWrap(), f) );
uses the precedence of the comma-operator to assure beginWrap() is called first. The result of f is passed to endWrap() which just returns it.
So the output is:
beginWrap()
foo 10.100000
endWrap()
And the result r contains 10.1.
You're out for a big challenge if you are looking to create a generic class that can wrap and call an arbitrary function. In this case you'd have to make the functor (the operator()) to return double and take an int as a parameter. Then you have created a family of classes that can call all functions with that same signature. As soon as you want to add more types of functions, you need more functors of that signature, e.g.
MyClass goo(double a, double b)
{
// ..
}
template<class Function>
class Timer {
public:
Timer(Function& fct)
: fct_(fct) {}
MyClass operator()(double a, double b){
}
};
EDIT: Some spelling errors
It's not really clear to me for what you are looking.. However, for the given example, it's simply:
void operator() (int x)
{
clock_t start_time = ::clock(); // time before calling
fct_(x); // call function
clock_t end_time = ::clock(); // time when done
elapsed_time_ += (end_time - start_time) / CLOCKS_PER_SEC;
}
Note: This will measure the time in seconds. If you want to have high-precision timers, you probably have to check OS specific functionality (like GetTickCount or QueryPerformanceCounter on Windows).
If you want to have a generic function wrapper, you should have a look on Boost.Bind that will help tremendeously.
If your compiler supports variadic macros, I'd try this:
class Timer {
Timer();// when created notes start time
~ Timer();// when destroyed notes end time, computes elapsed time
}
#define TIME_MACRO(fn, ...) { Timer t; fn(_VA_ARGS_); }
So, to use it, you'd do this:
void test_me(int a, float b);
TIME_MACRO(test_me(a,b));
That's off the cuff, and you'd need to play around to get return types to work (I think you'd have to add a type name to the TIME_MACRO call and then have it generate a temp variable).
Here's how I'd do it, using a function pointer instead of a template:
// pointer to a function of the form: double foo(int x);
typedef double (*MyFunc) (int);
// your function
double foo (int x) {
// do something
return 1.5 * x;
}
class Timer {
public:
Timer (MyFunc ptr)
: m_ptr (ptr)
{ }
double operator() (int x) {
return m_ptr (x);
}
private:
MyFunc m_ptr;
};
I changed it to not take a reference to the function, but just a plain function pointer. Usage remains the same:
Timer t(&foo);
// call function directly
foo(i);
// call it through the wrapper
t(i);
In C++ functions are first class citizens, you can literally pass a function as a value.
Since you want it to take an int and return a double:
Timer(double (*pt2Function)(int input)) {...