ForEach:: Call < > .
ForEach::Call<> : ForEach::Call<N>(function_object, arguments...)
function_object - , operator(), :
template <std::size_t Idx>
void operator()() { }
ForEach < > .
class ForEach {
public:
template <std::size_t N, typename F, typename... Args>
static void Call(F &&f, Args &&...args) {
Impl<0, N>()(std::forward<F>(f), std::forward<Args>(args)...);
}
private:
template <std::size_t Idx, std::size_t End>
class Impl;
template <std::size_t End>
class Impl<End, End> {
public:
template <typename F, typename... Args>
void operator()(F &&, Args &&...) { }
};
template <std::size_t Idx, std::size_t End>
class Impl {
public:
template <typename F, typename... Args>
void operator()(F &&f, Args &&...args) {
std::forward<F>(f).template operator()<Idx>(std::forward<Args>(args)...);
Impl<Idx + 1, End>()(std::forward<F>(f), std::forward<Args>(args)...);
}
};
};
SomeClass, .
template <std::size_t Size>
class Ints {
public:
using Data = std::array<int, Size>;
void AssignAll(int n) { ForEach::Call<Size>(Assign(), data_, n); }
void PrintAll() const { ForEach::Call<Size>(Print(), data_); }
private:
class Assign {
public:
template <size_t N>
void operator()(Data &data, int arg) const {
data[N] = arg;
}
};
class Print {
public:
template <size_t N>
void operator()(const Data &data) const {
std::cout << data[N] << std::endl;
}
};
Data data_;
};
Ints.
int main() {
Ints<5> ints;
ints.AssignAll(101);
ints.PrintAll();
}
101
101
101
101
101