Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2008-04-08 21:55:21


On Apr 8, 2008, at 9:40 PM, Steven Watanabe wrote:
> AMDG
>
> Howard Hinnant wrote:
>>> template <class T, class U>
>>> decltype(true ? T() : U()) Min(T&& a, U&& b) {
>>> return b < a ? b : a;
>>> }
>>>
>
> Should this really work if T of U is a reference type and thus cannot
> be default constructed?

You're right. Not by current language rules. The reported error
message could have been better.

By coincidence I'm dealing with a similar problem here: http://home.twcny.rr.com/hinnant/cpp_extensions/time2_demo.html
  , and dealing with it via a promote trait, quite similar to what
I've seen in boost. I.e. the Min signature might look like:

    template <class T, class U> typename promote<T, U>::type Min(T&&
a, U&& b);

With the *default* (and preferably standardized) promote trait looking
like:

template <class T, class U>
struct promote
{
private:
     static T t;
     static U u;
public:
     typedef decltype(true ? static_cast<T&&>(t) :
static_cast<U&&>(u)) type;
};

// promote is specializable for client-defined types

-Howard


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk