Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > declaring a friend template function whose arguments are of type = private member type

Reply
Thread Tools

declaring a friend template function whose arguments are of type = private member type

 
 
Hicham Mouline
Guest
Posts: n/a
 
      03-27-2009
hello,
I have

template<typename CurveTraits>
class TimeCurve1D
{
public:
typedef typename CurveTraits::tType tType;
typedef typename CurveTraits::fType fType;

fType GetSomething() const;
private:
typedef std:air<tType,fType>
EntryType;
typedef boost::array< EntryType, mContainerMaxSize > ContainerType;
ContainerType
m;
size_t
mContainerSize;
};

In GetSomething(), I use std::lower_bound( &m[0], &m[mContainerMaxSize],
std:air<t, NaN> )
lower_bound uses operator< by default

Where can I defined this operator< for EntryType?
and how can I declare it a friend inside TimeCurve1D?

I tried

template<typename CurveTraits>
class TimeCurve1D
{
.....
friend bool operator< <CurveTraits>( const EntryType&, const EntryType&);
.....
};


template<typename CurveTraits>
inline bool operator<( const typename TimeCurve1D<CurveTraits>::EntryType&
lhs,
const typename TimeCurve1D<CurveTraits>::EntryType&
rhs )
{
return lhs.first < rhs.first;
}


On instantiation, it said
typedef struct std:air<double, double> TimeCurve1D<...>::EntryType' is
private

but I am trying to make operator< friend...

rds,


 
Reply With Quote
 
 
 
 
Vladyslav Lazarenko
Guest
Posts: n/a
 
      03-27-2009
Basically, in your case EntryType type is always a specialization of
std:air<> template. That template defines “less than” comparator by
default, it looks like this (defined trough including 'utility' header
from STL):

template<class _T1, class _T2>
inline bool
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{ return __x.first < __y.first
|| (!(__y.first < __x.first) && __x.second < __y.second); }

So you are good as long as both types “CurveTraits::tType” and
“CurveTraits::fType” has “less than” operators defined. Note that you
don’t have to define “bool operator < (const EntryType & lhs, const
EntryType & rhs)” yourself.

For example, let’s assume you have the following code that uses your
“TimeCurve1D” template:

struct A { int v; };
struct B { double v; };

struct my_curve_traits {
typedef A tType;
typedef B fType;
};

In order to get it working you have to write the following operators:

bool operator < (const A & lhs, const A & rhs) { return lhs.v <
rhs.v; }
bool operator < (const B & lhs, const B & rhs) { return lhs.v <
rhs.v; }

Note that you don't have to define those operators for POD types (line
int, double etc.). This is an example:

#include <utility>
#include <stdexcept>
#include <iostream>

#include <boost/array.hpp>

template<typename CurveTraits>
class TimeCurve1D
{
public:
typedef typename CurveTraits::tType tType;
typedef typename CurveTraits::fType fType;

fType GetSomething() const { throw std::runtime_error("Not
implemented"); }

private:
enum { mContainerMaxSize = 128 }; // You probably forgot to define
mContainerMaxSize...

typedef std:air<tType,fType> EntryType;
typedef boost::array< EntryType, mContainerMaxSize > ContainerType;

ContainerType m;
size_t mContainerSize;
};

struct A { int v; };
struct B { double v; };

struct my_curve_traits {
typedef A tType;
typedef B fType;
};

typedef TimeCurve1D<my_curve_traits> my_curve_1d;

int main(int , char *[]) {
try {
my_curve_1d curve;
curve.GetSomething();
} catch(const std::exception & e) {
std::cout << "Here you go: " << e.what() << std::endl;
}
return 0;
}


Hope it helps!

---
Vladyslav Lazarenko
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
Declaring member function of another class as friend before it isdefined Cotangent Alpha C++ 0 06-27-2010 02:04 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
access private member function through a function having template type Mike C++ 9 12-29-2006 02:30 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM



Advertisments