T
titancipher
I have a container that I wish to allow the user to specify a custom
comparison method very similar to std::less. However, I want it to
function more like memcmp (returning -1 0 1), and I want to be able to
vary the fields that are compared. The example below shows how I'd
like it to fit together.
struct fields
{
fields( int f1, int f2, int f3 ){ m_f[0] = f1; m_f[1] = f2; m_f[2]
= f3; }
int m_f[3];
};
template< typename T >
class Foo
{
public:
Foo( const T & t ) : m_t( t ){ }
int dosomething( const T & t )
{
// error: want to compare t by any of the 3 fields. the method
// passed in should handle which fields are compared.
if( t < m_t ) return -1;
else if( t > m_t ) return 1;
else return 0;
}
protected:
const T m_t;
};
int main(int argc, char* argv[])
{
Foo< fields > foo( fields( 3, 2, 1 ) );
foo.dosomething( fields( 1, 2, 3 ) );
return 0;
}
comparison method very similar to std::less. However, I want it to
function more like memcmp (returning -1 0 1), and I want to be able to
vary the fields that are compared. The example below shows how I'd
like it to fit together.
struct fields
{
fields( int f1, int f2, int f3 ){ m_f[0] = f1; m_f[1] = f2; m_f[2]
= f3; }
int m_f[3];
};
template< typename T >
class Foo
{
public:
Foo( const T & t ) : m_t( t ){ }
int dosomething( const T & t )
{
// error: want to compare t by any of the 3 fields. the method
// passed in should handle which fields are compared.
if( t < m_t ) return -1;
else if( t > m_t ) return 1;
else return 0;
}
protected:
const T m_t;
};
int main(int argc, char* argv[])
{
Foo< fields > foo( fields( 3, 2, 1 ) );
foo.dosomething( fields( 1, 2, 3 ) );
return 0;
}