Reduce verboseness of parameter function lambda input / How use capture?

Is there any better syntax I could be using to reduce verboseness? For example, I know the [] is the "capture" but not sure how to use it or if it is applicable here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>

using namespace std;

void out_to_pointer(int *out, string s, 
function<void(int *i)> the_input_func = [](int *i) { return; }) {
	*out = stoi(s);
	the_input_func(out);
}

string assert(bool truth) { return truth ? "true" : "false"; }

int main() {
    int x;
    int y;
    int z;

    // reduce verboseness here?
    out_to_pointer(&x, "1234", [](int *i) { *i = min(*i, 3); });
    out_to_pointer(&y, "1234", [](int *i) { *i = max(*i, 3); });
    out_to_pointer(&z, "500");

    cout << "does x equal 3 ?    " << assert(x == 3) << endl;
    cout << "does y equal 1234 ? " << assert(y == 1234) << endl;
    cout << "does z equal 500 ?  " << assert(z == 500) << endl;
}


I just want to be able to do various things to the final value, such as set bounds, set a min or max, sometimes I also need to round to the nearest power of 2, sometimes I need to add or subtract from the value, and I'd like to do all that in the function input. There's no pattern to what I need to do, so I can't exactly pre-define the functions.
Last edited on
another idea I just had, make a class or something:

1
2
3
4
5
6
int x = out("1234").max(3);
int y = out("1234").min(3);
int y = out("1234").offset(-3);
int y = out("1234").clamp(3,100);
.rndpow2();
..etc
Last edited on
Topic archived. No new replies allowed.