Choosing between two functions

J

James Aguilar

I want to choose between using less and less_equal based on a boolean, but I
don't want to type the argument list twice. So I try to do something like the
following:

{
//...
binary_function<Date, Date, bool> &fn(inclusive ? less_equal<Date>()
: less<Date>());
return fn(d1, d2);
}

Assume that d1 and d2 are dates. How would one actually do this, as this causes
a syntax error: Error 1 error C2446: ':' : no conversion from 'std::less<_Ty>'
to 'std::less_equal<_Ty>' c:\docs\wkspc\vs\lab7\lab7\timedevent.cpp 59.

- JFA1
 
P

Pete Becker

James said:
{
//...
binary_function<Date, Date, bool> &fn(inclusive ? less_equal<Date>()
: less<Date>());
return fn(d1, d2);
}

Assume that d1 and d2 are dates. How would one actually do this, as this causes
a syntax error: Error 1 error C2446: ':' : no conversion from 'std::less<_Ty>'
to 'std::less_equal<_Ty>' c:\docs\wkspc\vs\lab7\lab7\timedevent.cpp 59.

{
//...
if (inclusive)
return less_equal<Date>()(d1, d2);
else
return less<Date>()(d1, d2);
}

Or, once you've got an implementation of TR1, like this:

std::tr1::function<bool,Date,Date> fn;
if (inclusive)
fn = less_equal<Date>();
else
fn = less<Date>();
return fn(d1, d2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,182
Latest member
LucaCastan

Latest Threads

Top