Multi-parameter addition and comparison of C + + to implement similar Common Lisp

Source: Internet
Author: User
Tags bool comparison

In CL we can do this:

$ SBCL
* (+ 1 2 3)
6
* (< 1 2 3)
T
* (< 2 3 1)
NIL
*

From a simple perspective, CL's + and < is a function that receives multiple parameters, somewhat similar to the 1,2,3 and less (1,2,3) of CPP.

So when the c++11 begins to have the variable parameter template, you can play multiple parameter addition and multiple parameter comparisons.

#include <functional>
template<typename o, typename a,typename b>
bool cmp (o, A a,b B) {
    Return O (A, b);
}
Template<typename O, TypeName a,typename b,typename ... c>
BOOL CMP (O o,a a,b b,c ... C) {return
    O (A, B) and CMP (O,b,c ...);
}
Template<typename O, TypeName a,typename b>
a reduce (o, a a,b B) {return
    O (A, b);
}
Template<typename O, TypeName a,typename b,typename ... c>
A Reduce (O o,a a,b b,c ... c) {return
    reduce (O,o (A, B), C ...);
}
   
BOOL Foo (int a,int b,int c,int d) {return
  cmp (std::less<int> (), a,b,c,d);
int bar (int a,int b,int c,int d) {return
  reduce (std::p lus<int> (), a,b,c,d);

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Some people may say, this is not a pit dad, regardless of the wording than CL ugly, you also recursive call, simply weak burst ...

Let's take a look at the Truth (clang):

foo (int, int, int, int):                             # @foo (int, int, int, int)
    Cmpl    %esi,%edi
    jge    . Lbb0_1
    Cmpl    %edx,%esi
    setl    %sil
    cmpl    ,%ecx,    %edx setl%al andb% SIL,%al
    ret
. Lbb0_1:
    xorl    %eax,%eax
    ret
   
bar (int, int, int, int):                             # @bar (int, int, int, int)
    ADDL    %esi,%edi
    Leal    (%RDI,%RDX),%eax
    addl    ,%ecx
    ret

and handwriting is absolutely not bad oh, a little bit ugly, but you see the assembly of CL, is certainly slower than C + + .... (Escape

Finally, what's the use?

Multi-parameter addition seems to be of no use to write A+b+c+d.

Multiple parameter comparisons are OK

A < b and B < C and C < D

Related Article

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.