tworząc template, chciałbym uniknąć pisania w pętli "for_each" print<int> lub print<string> wolałbym żeby tam stało samo słowo print
// (Debian 10.2.1-6) Code::Blocks 20.03 -std=c++17 or higher
// g++ -Wall -fexceptions -g -std=c++17 -c main.cpp -o main.o
#include <iostream>
#include <algorithm> // for_each
using namespace std;
template<typename T>
void print(T const& x) {
cout<<x<<' ';
}
int main()
{
int tab[] = {21, 20, 33, 43, 15, 36, 777, 18, 9, 210};
for_each(begin(tab),end(tab), [](const int &x){std::cout << x << " ";} );
cout<<'\n';
for_each(rbegin(tab),rend(tab), print<int>);
cout<<'\n';
string SolarSystem[] = { "Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
for_each(begin(SolarSystem),end(SolarSystem), print<string>);
cout<<'\n';
std::for_each(begin(SolarSystem),end(SolarSystem), [](const auto& value) {
print(value);
});
return 0;
}
reich