官术网_书友最值得收藏!

  • Mastering the C++17 STL
  • Arthur O'Dwyer
  • 147字
  • 2021-07-08 10:20:19

Concrete monomorphic functions

What distinguishes an abstract algorithm from a concrete function? This is best shown by example. Let's write a function to multiply each of the elements in an array by 2:

    class array_of_ints {
int data[10] = {};
public:
int size() const { return 10; }
int& at(int i) { return data[i]; }
};

void double_each_element(array_of_ints& arr)
{
for (int i=0; i < arr.size(); ++i) {
arr.at(i) *= 2;
}
}

Our function double_each_element works only with objects of type array_of_int; passing in an object of a different type won't work (nor even compile). We refer to functions like this version of double_each_element as concrete or monomorphic functions. We call them concrete because they are insufficiently abstract for our purposes. Just imagine how painful it would be if the C++ standard library provided a concrete sort routine that worked only on one specific data type!

主站蜘蛛池模板: 天全县| 保康县| 东平县| 柘城县| 石林| 顺平县| 合山市| 雷山县| 罗田县| 巩留县| 新津县| 台东县| 永吉县| 吉木萨尔县| 抚宁县| 开平市| 册亨县| 远安县| 库车县| 黔西县| 东平县| 丹江口市| 宜城市| 万安县| 上饶县| 石家庄市| 宝兴县| 安仁县| 二手房| 桃江县| 慈溪市| 鹿邑县| 遂川县| 华坪县| 京山县| 东宁县| 拉孜县| 葵青区| 温泉县| 宣恩县| 大同市|