PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

CUDA-x86.

Difficult C++ template code (decorator pattern)

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
dgront



Joined: 05 May 2005
Posts: 1

PostPosted: Thu May 05, 2005 4:25 am    Post subject: Difficult C++ template code (decorator pattern) Reply with quote

Here is a simple "Point" class with its accessor and decorator.
The program compiles easily with g++, but with pgCC doesn't

Is it my fault (can anyone find a reason?) or pgCC does not support C++ templates in full?

Dominik

#include <iostream>
#include <vector>

// Point data structure
template <typename T> class Point {
public:
typedef T x_type;
x_type x;
typedef T y_type;
y_type y;
typedef T z_type;
z_type z;

Point(void) { x = y = z = 0; }
Point(T nx,T ny,T nz) { x = nx; y = ny; z = nz; }

friend std::ostream& operator<<(std::ostream& o, const Point<T>& p){
o<<p.x<<" "<<p.y<<" "<<p.z<<"\n";
return o;
}
};

// An accessor class for the x field.
template< class T > class GetX {
public:
typedef T input_type;
typedef typename T::x_type output_type;
static output_type& get (input_type& v) { return v.x; }
static const output_type& get (const input_type& v) { return v.x; }
};

/*
* Decorate a given iterator to go over a specified field in a class
* Proper field is selected by accesor class.
*/
template< class Decorated, template< class > class Access >
class field_access: public Decorated {
public:
typedef typename Decorated::value_type value_type;
typedef Access< value_type > accessor;
typedef typename accessor::output_type output_type;
field_access () : Decorated () {}
field_access (const Decorated& d) : Decorated (d) {}

// Overload operator*, use the given accessor
// to get the proper field.
output_type& operator* () {
return accessor::get (Decorated::operator* ());
}
const output_type& operator* () const {
return accessor::get (Decorated::operator* ());
}
};

int main(void) {
GetX<Point<int> > accesor_ix; // Declare an accesor to its X coordinate
std::vector<Point<int> > c;

// Let's make some points
for(unsigned i=0;i<10;i++) {
Point<int> pi(i,i*2,i*3);
c.push_back(pi);
}

field_access< std::vector<Point<int> >::iterator, GetX > // Prepare iterators
i, begin = c.begin (), end = c.end ();
for(i=begin;i!=end;++i) // Iterate over X and change them a bit
std::cout<<(*i)<<"\n";
}
Back to top
View user's profile
Deborah Caruso



Joined: 20 Oct 2004
Posts: 43

PostPosted: Thu May 05, 2005 3:00 pm    Post subject: Reply with quote

dgront-

Our C++ compiler fully supports templates. Our STL iterator
struct, however is not defined in a manner that allows it to
be inherited. I have filed this as TPR 3533: we will fix this
to be compatible with gnu.

-Deborah Caruso
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group