std::conditional pick() function?

ยอดดู 177 ครั้ง
ข้ามไปที่ข้อความที่ยังไม่อ่านรายการแรก

Tony V E

ยังไม่อ่าน,
4 เม.ย. 2557 13:37:274/4/57
ถึง std-pr...@isocpp.org
Does anyone else (besides me) need a function like

    template<bool B, typename X, typename Y>
    std::conditional<B,X,Y>::type pick(X x, Y y);

that returns x or y (and type X or Y) based on the compile-time bool?

And if so, should it be part of std::conditional?  ie

current std::conditional (ie boost::if_c)

    template <bool cond, typename First, typename Second>
    struct conditional  // false case - second
    {
        typedef Second type;
    };
    template<typename First, typename Second>
    struct conditional<true, First, Second> // true - pick first
    {
        typedef First type;
    };

Proposal:

    template <bool cond, typename First, typename Second>
    struct conditional  // false case - second
    {
        typedef Second type;
        static Second pick(First , Second second)
        {
            return second;
        }
    };
    template<typename First, typename Second>
    struct conditional<true, First, Second> // true - pick first
    {
        typedef First type;
        static First pick(First first, Second )
        {
            return first;
        }
    };


Ville Voutilainen

ยังไม่อ่าน,
4 เม.ย. 2557 13:50:224/4/57
ถึง std-pr...@isocpp.org
On 4 April 2014 20:37, Tony V E <tvan...@gmail.com> wrote:
> Does anyone else (besides me) need a function like
>
> template<bool B, typename X, typename Y>
> std::conditional<B,X,Y>::type pick(X x, Y y);
>
> that returns x or y (and type X or Y) based on the compile-time bool?


I have certainly written a run-time version of it umpteen times. At those times
I didn't have the possibility to write a compile-time version. Whether that
is sufficient reason to standardize it is another matter. Very mildly
in favor...

Zhihao Yuan

ยังไม่อ่าน,
4 เม.ย. 2557 13:50:554/4/57
ถึง std-pr...@isocpp.org
On Fri, Apr 4, 2014 at 1:37 PM, Tony V E <tvan...@gmail.com> wrote:
> Does anyone else (besides me) need a function like
>
> template<bool B, typename X, typename Y>
> std::conditional<B,X,Y>::type pick(X x, Y y);
>
> that returns x or y (and type X or Y) based on the compile-time bool?

Nope, but if I do, I prefer to use C11 type generic expression:

#define PICK(cond, tv, fv) \
_Generic(bool_constant<bool(cond)>{}, \
std::true_type: (tv), \
std::false_type: (fv))

That gives you more flexibility, for example, you don't need to worry
about questions like "how to avoid the extra copy".

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://4bsd.biz/

Ville Voutilainen

ยังไม่อ่าน,
4 เม.ย. 2557 13:52:354/4/57
ถึง std-pr...@isocpp.org
On 4 April 2014 20:50, Zhihao Yuan <z...@miator.net> wrote:
> On Fri, Apr 4, 2014 at 1:37 PM, Tony V E <tvan...@gmail.com> wrote:
>> Does anyone else (besides me) need a function like
>>
>> template<bool B, typename X, typename Y>
>> std::conditional<B,X,Y>::type pick(X x, Y y);
>>
>> that returns x or y (and type X or Y) based on the compile-time bool?
>
> Nope, but if I do, I prefer to use C11 type generic expression:
>
> #define PICK(cond, tv, fv) \
> _Generic(bool_constant<bool(cond)>{}, \
> std::true_type: (tv), \
> std::false_type: (fv))
>
> That gives you more flexibility, for example, you don't need to worry
> about questions like "how to avoid the extra copy".


The way to avoid the "extra copy" is to move lvalues into the function,
and rvalues already avoid copies. I don't consider that a problem.

Zhihao Yuan

ยังไม่อ่าน,
4 เม.ย. 2557 14:07:344/4/57
ถึง std-pr...@isocpp.org
On Fri, Apr 4, 2014 at 1:52 PM, Ville Voutilainen
<ville.vo...@gmail.com> wrote:
>
> The way to avoid the "extra copy" is to move lvalues into the function,
> and rvalues already avoid copies. I don't consider that a problem.

In the first case, a std::move from the call side is required, plus
"move" is not doing nothing. And there is more,

PICK(blah, "\r\n", '\n')

-- No decay involved.

PICK(blah, lvalue, rvalue())

-- The value category is preserved.

Tony V E

ยังไม่อ่าน,
4 เม.ย. 2557 14:10:474/4/57
ถึง std-pr...@isocpp.org
On Fri, Apr 4, 2014 at 1:50 PM, Ville Voutilainen <ville.vo...@gmail.com> wrote:
On 4 April 2014 20:37, Tony V E <tvan...@gmail.com> wrote:
> Does anyone else (besides me) need a function like
>
>     template<bool B, typename X, typename Y>
>     std::conditional<B,X,Y>::type pick(X x, Y y);
>
> that returns x or y (and type X or Y) based on the compile-time bool?


I have certainly written a run-time version of it umpteen times.


The runtime version can't return the right type.  I need the type and the value, based on, ie is_integral<X>, etc.
I find it helps build Concept-like functionality.
 
At those times
I didn't have the possibility to write a compile-time version. Whether that
is sufficient reason to standardize it is another matter. Very mildly
in favor...

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

David Krauss

ยังไม่อ่าน,
5 เม.ย. 2557 02:18:415/4/57
ถึง std-pr...@isocpp.org
This sounds an awful lot like a C11 type-generic expression. A TGE uses the type of an unevaluated controlling expression to select among several subexpressions. The type and value of the TGE are those of the selected subexpression; the other subexpressions are not evaluated.

TGEs do more than std::conditional can do, but less than the proposed static_if. I think they just might be an appropriate solution.

- D

ตอบทุกคน
ตอบกลับผู้สร้าง
ส่งต่อ
ข้อความใหม่ 0 รายการ