Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Template loop unrolling Account Manager
 
TheComet

October 14, 2013, 07:11 AM

Hey all,

(How do I make code tags btw?)

I'm pretty new to C++ template meta-programming, and am going through an experimental phase with them right now. I came across this template loop unrolling code found here: http://www.flipcode.com/archives/Unrolling_Loops_With_Meta-Programming.shtml

Unfortunately, it will not compile. The problem is that in order for a templated function or class to be specialised, all outer templates must be specialised as well, which is not the case with the Loop template, seen here:

[code] template
struct Loop
{
inline static void exec( int* aLArray, int* aRArray ) {}
};[/code]

This is the entire code:

[code]//============================================================================
// The two following templated classes contain a static member function (doOperation)
// performing assignation operation between two values(= or +=).
// so T_lhs and T_rhs can be float, const float, double, a Complex class or whatever.
//============================================================================
struct AssignOpAssign
{
template
inline static void doOperation( T_lhs& lhs, T_rhs& rhs ){ lhs = rhs; }
};
struct AssignOpAdd
{
template
inline static void doOperation( T_lhs& lhs, T_rhs& rhs ){ lhs += rhs; }
};

//============================================================================
// The AssignOpLoopUnroller class contains the code performing the loop unrolling
// using the template recursion principle.
// This principe is simple: a template with an integer parameter contains a static
// function exec. Loop::exec calls Loop::exec
// Explicit specialisation is used to stop the recursion: Loop::exec() does nothing.
//============================================================================
template
struct AssignOpLoopUnroller
{
template
struct Loop
{
inline static void exec( int* aLArray, int* aRArray )
{
aLArray[N-1] = aRArray[N-1];
Loop::exec( aLArray, aRArray );
}
};
template
struct Loop
{
inline static void exec( int* aLArray, int* aRArray ) {}
};
};

// simple test
int main()
{
int arr1[8] = {2,3,8,4,4,2,8,5};
int arr2[8] = {6,2,7,4,3,5,6,7};
AssignOpLoopUnroller::Loop::exec( arr1, arr2 );
return 0;
}[/code]

I'm using gcc 4.8.1 and am running Linux.

What options do I have to fix this?

 
This thread contains 1 message.
 

If you'd like to post a comment on this discussion thread, please log in or create an account.

 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.