0

I am not able to understand the meaning of this shortcut line. Even also not able to understand what is used here. thanks in advance.

I was understanding someones other code but I searched all around but didn't get it.

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

2 Answers2

3

It's a function template.

mini(a, b);

is the same as

a = min(a, b);

(where min is presumably std::min).

Because it's a function template a and b can be any type (as long as they are the same type).

Seems like a fairy worthless piece of code to me, why not just write a = min(a, b); but I don't know the context.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    The main difference is that `mini()` forces the first argument to be a variable what's not the case for `std::min()`. Hence, `int a = std::min(1, 2);` would work but `mini(1, 2);` wouldn't. (I must admit that I've no idea for what the restriction could be good for.) ;-) – Scheff's Cat Jun 02 '19 at 06:52
  • 1
    @Scheff - It's a mutator. I suspect they are meant to `mini`mize and `maxi`mize their respective first argument. – StoryTeller - Unslander Monica Jun 02 '19 at 07:02
  • @StoryTeller Something like an "anti-functional" programming pattern? Actually, I like the partly functional paradigm parts of C and C++. For certain tasks, they are really useful... ;-) – Scheff's Cat Jun 02 '19 at 07:04
  • To me it looks like a pure convenience function just to spare the assignment. Good idea? Spares some typing for the price of worse readability (new, unknown function introduced in comparison to well-known `std` function), so I wouldn't approve... – Aconcagua Jun 02 '19 at 07:07
  • @StoryTeller "Mutator" was a new term for me. I googled a bit but found only "mutator"s in the sense of what I had called "setter" methods... – Scheff's Cat Jun 02 '19 at 07:09
  • 2
    @Scheff - It's a generalization of a "setter" AFAIK. Doesn't have to accept a "new value" or be a member. Just has to modify the object's state. – StoryTeller - Unslander Monica Jun 02 '19 at 07:15
3

As this tutorial explained, this is a Function Template. The general form of a function template definition is shown here :

template <class type> ret-type func-name(parameter list) {
   // body of function
}

The min and max macros used in these function templates that defined as :

#define min(a,b)            (((a) < (b)) ? (a) : (b))
#define max(a,b)            (((a) > (b)) ? (a) : (b))

So in these function templates, The minimum and maximum number between a4 and b4 are detected, then returned in C &a4 :

template<class C> void mini(C &a4, C b4)
{
    a4 = min(a4, b4);
}

template<class C> void maxi(C &a4, C b4)
{
    a4 = max(a4, b4);
}

In this example I defined it with the int and the float data-types (class C in the function template):

int main()
{
    int i1 = 1;
    int i2 = 2;

    float f1 = 12.5;
    float f2 = 12.4;

    mini<int>(i1, i2);
    maxi<float>(f2, f1);

    std::cout << i1 << ", " << i2 << ", " << f1 << ", " << f2 << std::endl;

    return 0;
}

The result is :

1, 2, 12.5, 12.5

Notice : The min and mx macros defined in windows.h file by default.... but these macros are simple and you can define them by yourself instead and not using of windows header file. And I recommend using of std::min and std::max that are for c++

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • I'd recommend to swap the sample values – `a` would then get modified, so output more obvious... And I'd add at least a note that template arguments can be deduced, so you wouldn't have to explicitly specify (unless types of a and b differed). – Aconcagua Jun 02 '19 at 07:10
  • That's more than I actually intended :) As you now have two examples, I'd yet drop the template argument for second call (only!) to illustrate that we can let the compiler deduce it (and add an appropriate comment). – Aconcagua Jun 02 '19 at 07:24
  • In follow-up to @Scheff's comment: About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... And your note (still following Scheff) would break the code on my system, there simply isn't any `windows.h`... (I'm on linux.) – Aconcagua Jun 02 '19 at 07:26
  • @Aconcagua due to using of `min` and `max` macros we have to include `windows.h`... – BattleTested_закалённый в бою Jun 02 '19 at 07:32
  • I'd rather assume that OP has forgotten to include the appropriate using directive (we recommend not to use) and actually uses the [std](https://en.cppreference.com/w/cpp/algorithm/min) variants. At least I'd assume that the code *is* compiling already (it's a question of type "what is this?", not of "why doesn't this compile?"), so even *if* using windows defines (which migh [break standard](https://stackoverflow.com/a/11544154/1312382) variants, though) the windows header most likely already *is* included. – Aconcagua Jun 02 '19 at 07:46
  • @Scheff `min` and `max` are two simple macros and as you see, I put their definitions in my answer and you can use of them instead of includeing `windows.h` – BattleTested_закалённый в бою Jun 02 '19 at 07:46
  • @Scheff Instead of undefining min/max, likely better option is adding -DNOMINMAX to compiler flags... – Aconcagua Jun 02 '19 at 07:50
  • 2
    By the way – using `std::min`/`std::max` is superior to the macros anyway (in *general*, not if wrapped into another function as in question): `auto m = min(someFunction(), anotherFunction())` with your macros results in calling one of the functions twice... – Aconcagua Jun 02 '19 at 07:54