Skip to content
Snippets Groups Projects
Commit 0a926954 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Writing TemplateStaticFor documentation.

parent 4c64b1fa
No related branches found
No related tags found
1 merge request!44Tutorials
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/StaticVector.h>
#include <TNL/Algorithms/StaticFor.h>
#include <TNL/Algorithms/TemplateStaticFor.h>
using namespace TNL;
using namespace TNL::Containers;
const int Size( 5 );
template< int I >
struct LoopBody
{
static void exec( const StaticVector< Size, double >& v ) {
std::cout << "v[ " << I << " ] = " << v[ I ] << std::endl;
}
};
int main( int argc, char* argv[] )
{
/****
* Create two static vectors
* Initiate static vector
*/
const int Size( 3 );
StaticVector< Size, double > a, b;
a = 1.0;
b = 2.0;
double sum( 0.0 );
StaticVector< Size, double > v{ 1.0, 2.0, 3.0, 4.0, 5.0 };
/****
* Compute an addition of a vector and a constant number.
* Print out the vector using template parameters for indexing.
*/
auto addition = [&]( int i, const double& c ) { a[ i ] = b[ i ] + c; sum += a[ i ]; };
Algorithms::StaticFor< 0, Size >::exec( addition, 3.14 );
std::cout << "a = " << a << std::endl;
std::cout << "sum = " << sum << std::endl;
Algorithms::TemplateStaticFor< 0, Size, LoopBody >::exec( v );
}
......@@ -17,6 +17,31 @@
namespace TNL {
namespace Algorithms {
/**
* \brief TemplateStaticFor serves for coding for-loops in template parameters.
*
* The result of calling this loop with a templated class \p LoopBody is as follows:
*
* LoopBody< begin >::exec( ... );
*
* LoodBody< begin + 1 >::exec( ... );
*
* ...
*
* LoopBody< end - 1 >::exec( ... );
*
* \tparam IndexType is type of the loop indexes
* \tparam begin the loop iterates over index interval [begin,end).
* \tparam end the loop iterates over index interval [begin,end).
* \tparam LoopBody is a templated class having one template parameter of IndexType.
*/
template< typename IndexType,
IndexType begin,
IndexType end,
template< IndexType > class LoopBody >
struct TemplateStaticFor;
namespace detail {
template< typename IndexType,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment