C++14 Generic lambdas in header file

1.1k Views Asked by At

I have set of functors like the following:

const auto add = [](const auto& x) {
    return [=](const auto& n) { return n + x; };
};

Is it right to store them in a header file? (any side effects?)

1

There are 1 best solutions below

1
On BEST ANSWER

You could store them in a header with no problem at all. IF you have the same function with the same arguments it might cause a problem, but if you have different names or arguments, it overloads it and it has no problem.

As for consts, they could be stored in headers simply to use them later in different programs. Just as functions, you could use the constant (defined by you) whenever you need it.

As "side effects" I would say that you could indlude the header in another file and use your function without having to redeclare it.