boost中的operator及一些探讨

news/2023/12/1 7:39:29

在generic programming中,我们往往希望自己定义的type在行为上和C++内置的类型尽可能的相似,也就是说,可以参与各种各样的表达式运算而不会有阻碍。C++为我们提供强大的运算符重载机制也就是为了这个目的。不幸的是,重载运算符往往是一件枯燥无聊的事情,我们可以大致统计一下,要完全定一个type,我们需要重载的运算符:
算术运算:+, -, *, /, %, +=, -=, *=, /=, /, %=
比较运算:>, <, ==, !=, >=, <=
位运算:|, &, ^, |=, &=, ^=
自增自减:++, -- 它们放在变量前面和后面是不同的,这就是4个
……
这只是对于该type本身,如果你希望定义这个type和另外一个type的混合运算——例如你定义了一个复数,自然要定义它和int, double, float...之间的运算——那么你就要把上面的运算符再来上那么几次,注意,如果要实现可交换的运算(例如加法,乘法),重载数量还要乘以2……

实际上,我们可以发现,在通常情况下这些运算中的很多都是可以用其他的运算来达到的,例如
>= 可以是 >||=,当然也可以是 !< 等等。
除非你的type的表现比较奇怪(例如matrix的乘法不对称),否则只要实现了一些基本的运算符,其余的就可以用某种机制来自动实现了。boost在operator.hpp中提供了这种机制,它利用了C++强大的继承、模版和宏。

在下面的代码中,尽管没有直接定义MyInt的+和*操作,但是由于从additive和multiplicative派生,所以这些操作都被自动定义了,我们只需要定义+=和*=即可。

#include

using namespace boost;

class MyInt
 : additive  , multiplicative >
{
public:
    int i;

    typedef MyInt self;

    MyInt(){};

    MyInt(const MyInt& mi)
    {
        i = mi.i;
    }

    self& operator+=(const int ii)
    {
        i += ii;
        return *this;
    }

    self& operator*=(const int ii)
    {
        i *= ii;
        return *this;
    }
};


#include
using namespace std;

void main()
{
    MyInt i;
    i.i = 10;
    int j = 5;

    MyInt k = i + j;
    MyInt l = j + i;
    MyInt m = i * j;
    MyInt n = j * i;
}


对于比较和逻辑运算,也有相应的operator基类模版可以继承。值得注意的是继承它们的方法:

class MyInt
 : additive  , multiplicative >


这里不是直接的多继承,而是把multiplicative作为additive的第3个模版参数,而additive的实现中,是从第3个模版参数派生的(由于additive是通过macro来定义的,所以这里选了另一个相似的东西less_than_comparable2来作参照):

template
struct less_than_comparable2 : B

换句话说,继承关系可以看成是这样的(实际上还有很多中间层次,这里省略):
::boost::detail::empty_base
                ^
        mutiplicative
                ^
           additive
                ^
            MyInt
如果采用直接的多继承,也就是如果这样写,

class MyInt
 : additive
 , multiplicative


那么继承关系可能是这样的:
::boost::detail::empty_base        ::boost::detail::empty_base
                ^                                                ^
        mutiplicative                                   additive
                ^                                                ^
                                        MyInt
多出了一个empty_base,虽说empty_base是空的,这样做可能没有多大的消耗,但是在类层次复杂的时候,也许就会有比较大的浪费了。





http://www.niftyadmin.cn/n/3655038.html

相关文章

LeetCode 之 JavaScript 解答第一题 —— 两数之和(Two Sum)

Time&#xff1a;2019/4/1 Title:Two Sum Difficulty: simple Author:小鹿 题目一&#xff1a;Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one s…

仿照boost::lexical_cast,编写一个text_cast

首先说明&#xff0c;这个text_cast不光是编写来玩的&#xff0c;它还有一定的用途。我在最近的一个跨平台&#xff08;Win32&#xff0c;数个版本的Linux&#xff09;的项目中用到了boost库&#xff0c;编码的时候还是很爽的&#xff0c;等到了移植的时候&#xff0c;就发现我…

LeetCode 之 JavaScript 解答第二题 —— 两数相加(Add Two Numbers)

Time&#xff1a;2019/4/2 Title: ADD Two Numbers Difficulty: medium Author:小鹿 公众号&#xff1a;一个不甘平凡的码农。 题目二&#xff1a;ADD Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored …

VC7.1 编译器的一个不大不小的bug

这段代码在任何一个成熟的C编译器当中都不会通过&#xff1a;class Test{public: char *p;static void Tt() { p 0; }};因为它在static函数中访问实例成员&#xff0c;而在调用static函数的时候&#xff0c;可能根本就没有实例存在&#xff0c;即便存在&#…

LeetCode 之 JavaScript 解答第十五题 —— 三数之和(3Sum)

Time&#xff1a;2019/4/3 Title:3Sum Difficulty: medium Author:小鹿 题目三&#xff1a;ADD Two Numbers Given an array nums of n integers, are there elements a, b, c in nums such that a b c 0? Find all unique triplets in the array which gives the sum of …

LeetCode 之 JavaScript 解答第169题 —— 求众数 I(Majority Element)

Time&#xff1a;2019/4/4 Title: Majority Element 1 Difficulty: easy Author: 小鹿 题目&#xff1a;Majority Element 1 Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋times. You may as…

元编程技法(1)——if_c

一直想整理一下对于meta-programming的一些想法&#xff0c;就从这个最简单的开始吧&#xff01;if_c是boost::mpl库提供的一个元编程算法&#xff0c;它接受三个模板参数&#xff0c;第一个是bool类型&#xff0c;后面两个是类型名&#xff1a;template< bool c ,…

LeetCode 之 JavaScript 解答第229题 —— 求众数 II(Majority Element)

Time&#xff1a;2019/4/5 Title: Majority Element 2 Difficulty: medium Author: 小鹿 问题&#xff1a;Majority Element 2 Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time…

关于 Boost.Lambda

Boost.Lambda 的确是一个好东西&#xff0c;用来举例最多的恐怕就是这个了&#xff1a;using namespace boost::lambda;std::vectorv;// init valuesstd::for_each(v.begin(), v.end(), std::cout << _1 << );精致、优雅、易于理解&#xff0c;下面是一个更漂亮的…

LeetCode 之 JavaScript 解答第41题 —— 缺失的第一个正数(First Missing Positive)

Time&#xff1a;2019/4/6 Title: First Missing Positive Difficulty: Difficulty Author: 小鹿 题目&#xff1a;First Missing Positive Given an unsorted integer array, find the smallest missing positive integer. Note: Your algorithm should run in O(n) time a…

泛型算法:Tips (1) --- bind 基础

从 STL 出现到现在已经这么多年了&#xff0c;泛型算法是它的重要组成&#xff0c;也是其中最“看起来很美”的东西之一。然而在真实的程序设计中&#xff0c;它往往成为程序员的心头一痛&#xff0c;因为一旦要用 for_each &#xff0c;accumulate 之类的算法做一些稍微复杂一…

LeetCode 之 JavaScript 解答第141题 —— 环形链表 I(Linked List Cycle I)

Time&#xff1a;2019/4/7 Title: Linked List Cycle Difficulty: Easy Author:小鹿 题目&#xff1a;Linked List Cycle I Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represen…

泛型算法:Tips (2) --- 累加

如果你想要把一个容器内的所有元素累加起来&#xff0c;应该怎么办&#xff1f;STL 的 accumulate 可以让我们不必自己写循环&#xff1a;#include #include #include #include #include int main(){std::vectorvect;vect.push_back(1);vect.push_back(2);vect.push_back(3);ve…

LeetCode 之 JavaScript 解答第142题 —— 环形链表 II(Linked List Cycle II)

Time&#xff1a;2019/4/8 Title: Linked List Cycle II Difficulty: medium Author:小鹿 题目&#xff1a;Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given lin…

泛型算法:Tips (3) --- 初始化

上次提到过为容器生成数据的问题&#xff0c;我给出的用 boost.lambda 的方法是&#xff1a; std::vectorvect(10);int i 0; std::for_each( vect.begin(), vect.end(), _1 var(i) );不错&#xff0c;这样可以生成连续的数字&#xff0c;也还算比较简洁&#xff0c;因为代码…

TopLanguage小组讨论精选[三](2007.11-2007.12)

TopLanguage小组讨论精选[三](2007.11-2007.12)By 刘未鹏(pongba)C的罗浮宫(http://blog.csdn.net/pongba)TopLanguage小组的成员已经超过28(279)啦&#xff01;char已经存不下了&#xff0c;该换int了:-) 而讨论则已经超过了211(2379)&#xff01;最近很忙&#xff0c;没空写b…
最新文章