从masonry中学到的c++and关键字

戴维营教育原创文章,转载请注明出处。我们的梦想是做最好的iOS开发培训!

一直在用Masonry对视图进行自动布局(AutoLayout),它的串联设置的方式很方便,多个相同的约束条件可以用and进行连接。今天@大茶园丁在测试MobileVLCKit时突然发现and变红了,貌似是一个关键字,并且编译也通不过。于是百度确认了一下,结果在@余璜同学的博客上早就介绍了(顺便汗颜了一下,一直说的坚持写博客)。

[v mas_makeConstraints:^(MASConstraintMaker *make) {
    v.left.and.right.equalTo(@20);
}];

一、C语言

其实在C语言中就已经定义了许多宏,用来增加一些运算符的可读性,只是用的人比较少(iso646.h)。我们只要包含这个头文件就可以使用and、or之类的作为运算符了。

#ifndef __ISO646_H
#define __ISO646_H

#ifndef __cplusplus
#define and    &&
#define and_eq &=
#define bitand &
#define bitor  |
#define compl  ~
#define not    !
#define not_eq !=
#define or     ||
#define or_eq  |=
#define xor    ^
#define xor_eq ^=
#endif

#endif /* __ISO646_H */
#include <stdio.h>
#include <iso646.h>

int main(int argc, char *argv[]) {
    int a = 0;
    int b = 1;
    //使用and代替&&
    if(a and b) {
        printf("真\n");
    }
    else {
        printf("假\n");
    }
    return 0;
}

直接用clang进行编译。

$ clang keywords.c -o keyword
$ ./keyword
假

二、C++

不过在C++中就更简单了,直接成为了关键字,使用它们的时候不需要添加任何头文件。

#include <iostream.h>

int main(int argc, char *argv[]) {
    int a = 0;
    int b = 1;
    //使用and代替&&
    if(a and b) {
        std::cout << "真" << std::endl;
    }
    else {
        std::cout << "假" << std::end;
    }
    return 0;
}
$ clang keywords.cpp -o keyword
$ ./keyword
假

参考资料

https://zh.wikipedia.org/wiki/C替代标记
戴维营学院(高级开发视频): http://v.diveinedu.com

潜心俱乐部(iOS面试必备): http://divein.club

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Product Description The author uses practical, concise code examples to illuminate a useful programming stratagem or warn against a dangerous practice. Readers will come away with a better understanding of how C++ is used in the real world. From the Inside Flap In the hands of an expert, C++ helps designers and programmers build systems that are modular, maintainable, and fast. To the novice, however, the size of the language can be intimidating. There are a lot of features in C++ and it takes some experience to learn which ones are appropriate for any situation. This book is intended to enhance and expedite that learning process. Most successful C++ programmers cannot recite chapter and verse from the language rules; instead, they have acquired a set of idioms and techniques that have worked well for them. Our goal is to help the C++ novice learn those idioms that have been most useful in practice. We also point out some of the most common pitfalls. We do not try to cover the entire language and we leave the ultra-precise definitions of language semantics to the reference manuals. Instead, we concentrate on helping the reader build programs that can be understood by someone who is not a C++ language lawyer. We not only discuss techniques for making programs elegant and fast; we also show how to make them easier to understand and maintain. Acknowledgements Almost none of the ideas and programming idioms in this book are my invention. My goal has been to present, in a way that allows novice C++ programmers to learn them quickly, what I consider to be the most important strategies and tactics I have learned from others in the eight years I have been using C++. Some of these lessons were learned by studying actual development projects as they moved from C to C++; others came from discussions with talented individuals. Many of the best ideas on templates and library design, including the ideas behind many of the container classes in this book, came from classes in the USL Standard Components that were originally designed by Martin Carroll, Andrew Koenig, and Jonathan Shopiro. I claim exclusive ownership of any errors in my versions. Andrew Koenig was a valuable resource as the local C++ language lawyer. The participants in the "C++ Strategies and Tactics" seminars I presented at several conferences helped inspire this book and refine its ideas. Other important ideas came from Tom Cargill, John Carolan, Jim Coplien, Mark Linton, Gerald Schwarz, and of course Bjarne Stroustrup, who also invented the C++ programming language that made the book possible in the first place. Brian Kernighan read several drafts of this book, and his excellent feedback has been a lot of help. I would also like to thank David Annatone, Steve Buroff, Tom Cargill, Bill Hopkins, Cay Horstman, Lorraine Juhl, Peter Juhl, Stan Lippman, Dennis Mancl, Scott Meyers, Barbara Moo, Lorraine Weisbrot Murray, Bjarne Stroustrup, Clovis Tondo, Steve Vinoski, and Christopher Van Wyk for their comments on early drafts of this book. Lorraine Weisbrot Murray also contributed the encouragement, understanding, support, and love that helped make the entire effort feasible. Rob Murray
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值