C ++ implements multi-parameter addition and comparison similar to Common Lisp

Source: Internet
Author: User

In CL, we can do this:

1 $ sbcl2 * (+ 1 2 3)3 64 * (< 1 2 3)5 T6 * (< 2 3 1)7 NIL8 * 

In simple terms, the + and <of CL is a function that receives multiple parameters, a bit similar to the add (, 3) and less (, 3) of cpp.

Therefore, after the variable parameter template is available in C ++ 11, you can add multiple parameters and compare multiple parameters.

 1 #include <functional> 2 template<typename O, typename A,typename B> 3 bool cmp(O o, A a,B b){ 4     return o(a, b); 5 } 6 template<typename O, typename A,typename B,typename... C> 7 bool cmp(O o,A a,B b,C... c){ 8     return o(a, b) and cmp(o,b,c...); 9 }10 template<typename O, typename A,typename B>11 A reduce(O o, A a,B b){12     return o(a, b);13 }14 template<typename O, typename A,typename B,typename... C>15 A reduce(O o,A a,B b,C... c){16     return reduce(o,o(a, b),c...);17 }18 19 bool foo(int a,int b,int c,int d){20   return cmp(std::less<int>(), a,b,c,d);21 }22 int bar(int a,int b,int c,int d){23   return reduce(std::plus<int>(), a,b,c,d);24 }

Some people may say that this is not a pitfall. no matter whether the writing method is ugly than CL, you still call it recursively. It's so weak .....

Let's look at the truth (clang ):

 1 foo(int, int, int, int):                             # @foo(int, int, int, int) 2     cmpl    %esi, %edi 3     jge    .LBB0_1 4     cmpl    %edx, %esi 5     setl    %sil 6     cmpl    %ecx, %edx 7     setl    %al 8     andb    %sil, %al 9     ret10 .LBB0_1:11     xorl    %eax, %eax12     ret13 14 bar(int, int, int, int):                             # @bar(int, int, int, int)15     addl    %esi, %edi16     leal    (%rdi,%rdx), %eax17     addl    %ecx, %eax18     ret

It's no different from handwriting. Oh, it's a little ugly, but it's definitely slower than C ++ to read the CL compilation... (Escape

Finally, what is the purpose?

Multi-parameter addition seems useless. It is better to write a + B + c + d.
Multiple parameters are okay.

A <B and B <c and c <d

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.