Iterate over template classes in c++ 11 - c++

Suppose, I have class with such definition:
template<unsigned int N>
class A { ... }
The question is how to iterate over this classes with N?
for(unsigned int i = 0; i < 10; ++i) {
A<i>().doStuff();
}
Maybe there is some new feature in C++ 11 or some cool using of contrexp .
And next question is: if it's possible - how to store such classes?
Update
I know that it works at compile time. Suppose, I have up to 10 such global classes, which differs only in N. For example:
A<1> first;
A<2> second;
A<42> third;
A<1034> fourth;
And suppose, I should call the one who's got N bigger than my value. If there is no chances to iterate, so I have to write long if-else structure.
void doAppropriateStuff(int value) {
if (value < 1) {
first.doStuff();
} else if (value < 2) {
second.doStuff();
} else if (value < 42) {
third.doStuff();
} else if (value < 1034) {
fourth.doStuff();
} else {
...
}
}
Hope, the problem became clearer.
As I googled that's impossible and I understand why. Only hopes on C++11 and SO community.
Thanks.

It's obviously impossible with a for loop, because that's ran at runtime and template arguments need to be compile time constants. Here's how you could do it.
These are utility classes for constructing a sequence of integers as a template argument pack:
template< std::size_t... Ns >
struct indices {
typedef indices< Ns..., sizeof...( Ns ) > next;
};
template< std::size_t N >
struct make_indices {
typedef typename make_indices< N - 1 >::type::next type;
};
template<>
struct make_indices< 0 > {
typedef indices<> type;
};
The function that does the work:
#include <initializer_list>
template<size_t... Is>
void foo(indices<Is...>)
{
auto list = { (A<Is>().doStuff(), 0)... };
}
And you call the function like this:
foo(make_indices<10>::type());

If you do not want to rely on integer_sequence that is c++14, this is a simpler solution :
#include <iostream>
template<unsigned int N>
struct A {
void dostuff() const { std::cout << N << " "; }
};
template < int N > void run();
template <> void run<-1>() {}
template < int N > void run() {
run<N-1>();
A<N>{}.dostuff();
}
int main() {
run<10>();
}
EDIT : about your question update, you can do that if you store the objects inside a tuple, see here :
#include <iostream>
#include <tuple>
template<unsigned int N>
struct A {
unsigned int getN() const { return N; }
void dostuff() const { std::cout << N << " "; }
};
auto globals = std::make_tuple( A<3>{}, A<7>{}, A<10>{}, A<200>{} );
template <int idx> void run( int v );
template <> void run<std::tuple_size<decltype(globals)>::value>( int ) {}
template <int idx = 0> void run( int v ) {
auto & a = std::get<idx>(globals);
if ( v < a.getN() ) {
a.dostuff();
} else {
run<idx+1>(v);
}
}
int main() {
for( int i = 0; i<20; ++i)
run( i );
}

You would use template specialization:
template <unsigned int N> struct ADoer
{
static void go() { A<N>().doStuff(); ADoer<N - 1>::go(); }
};
template <> struct ADoer<0>
{
static void go() { }
};

Related

How can I use iterator values in templates?

Consider the following code:
#include <iostream>
enum class E
{
A,
B
};
template<E e> int f();
template<> int f<E::A>(){ return 1; }
template<> int f<E::B>(){ return 2; }
int main()
{
for( const E i : {E::A, E::B} )
{
std::cout << f<i>() << "\n";
}
}
This fails to compile, because i is not initialised with a constant expression. Is it possible make this idea of a code work?
This is very related: Why isn't a for-loop a compile-time expression?.
i in your loop is not a constant expression. However, by stealing from this answer one can make your code call f<i> inside the loop. It is not directly what you asked for because the proposed solution is for size_t index based loops instead of iterators, but it does call f<i> for all values of the enum:
#include <iostream>
#include <utility>
// your code
enum class E
{
A,
B
};
template<E e> int f();
template<> int f<E::A>(){ return 1; }
template<> int f<E::B>(){ return 2; }
// https://stackoverflow.com/a/47563100/4117728
template<std::size_t N>
struct num { static const constexpr auto value = N; };
template <class F, std::size_t... Is>
void for_(F func, std::index_sequence<Is...>)
{
using expander = int[];
(void)expander{0, ((void)func(num<Is>{}), 0)...};
}
template <std::size_t N, typename F>
void for_(F func)
{
for_(func, std::make_index_sequence<N>());
}
// emulate constexpr for (size_t i=0;i<2;++i) f<i>();
int main()
{
for_<2>([&] (auto i) {
std::cout << f<static_cast<E>(decltype(i)::value)>();
});
}
Output:
12
Probably there is a simpler solution for your actual problem. The answer to your literal question is: No. You cannot call f<i> when i is not a compile time constant. For detailed explanation I refer you to the above mentioned q&a.
The compiler is not able to deduce i in the call to f<i>(). This is because compilation happens before runtime, and the value of i is only available during runtime. You can work around this by adding a translator.
#include <iostream>
enum class E
{
A,
B
};
template<E e> int f();
template<> int f<E::A>(){ return 1; }
template<> int f<E::B>(){ return 2; }
int bar(E e)
{
int retval = 0;
switch(e)
{
case E::A:
{
return f<E::A>();
break;
}
case E::B:
{
return f<E::B>();
break;
}
default:
{
break;
}
}
return retval;
}
int main()
{
for( const E i : {E::A, E::B} )
{
std::cout << bar(i) << "\n";
}
return 0;
}

Can you call a compile-time instantiated template function with a run-time specified variable?

I have a function with an integer template parameter:
template<int N>
int func() {
return N;
}
I explicitly instantiate the template for all template arguments I might provide:
template int func<1>();
template int func<2>();
template int func<3>();
//...
I then want to call func with a run-time specified integer n:
//This code does not compile
int main() {
int n;
std::cin >> n;
// check that n is one of the allowed values
std::cout << func<n>(); // can fail if given a bad n
return 0;
}
Are there modifications I can make to this code to call func<n>() with n specified at run-time (say 0 < n < 20)?
Yes, it's possible, but not quite the way you're trying, because the template syntax like F<N> requires compile time values. However, once instantiated, they are regular functions and you can take function pointers to them, or put them in std::function, etc.
You can, for example, build an array for function pointers that allow access by a runtime index, something as follows:
template <int N>
int f() { return N; } // your function(s)
int main()
{
using Func = int(*)();
Func funcs[]{f<0>, f<1>, f<2>, }; // array of instances of f
// call a function via runtime value stored in n:
int n = 2;
funcs[n]();
}
Give the runtime program a list of functions to choose from and have it pick using an index (live example):
template<std::size_t... Indices>
int call_func_impl(std::index_sequence<Indices...>, int n) {
using FuncType = int(*)();
static const FuncType funcs[] = {func<Indices>...};
return funcs[n]();
}
template<int MaxN>
int call_func(int n) {
return call_func_impl(std::make_index_sequence<MaxN>{}, n);
}
...
call_func<20>(n);
Naturally, you could create the array manually (func<0>, func<1>, ...), but this wraps it up a bit more nicely. If you need a sequence other than 0 through N-1, use std::integer_sequence. If you need a non-sequence, there's no ready-made helper, though you can adapt one of the above if there's a pattern to arrive at the values you want (e.g., multiply each index_sequence element by 2 to get even numbers).
For completeness, C++20 enables a lovely (at least if you're used to worse) pattern for reducing copy-paste arguments:
template<int MaxN>
int call_func(int n) {
const auto impl = [n]<std::size_t... Indices>(std::index_sequence<Indices...>) {
using FuncType = int(*)();
static const FuncType funcs[] = {func<Indices>...};
return funcs[n]();
};
return impl(std::make_index_sequence<MaxN>{});
}
Here is a solution with simple caller function allowing to call your func when using integers from 1 to 20
#include <cassert>
#include <iostream>
template<int N>
int func() { return N; }
template <int N>
int callFunc(int n)
{
if (n==N) return func<N>();
else return callFunc<N-1>(n);
}
template <>
int callFunc<0>(int n)
{
assert(false);
return 0;
}
#define MAX_N 20
int main() {
int n;
std::cin >> n;
std::cout << callFunc<MAX_N>(n);
return 0;
}
If you can use C++17, another way using folding
#include <utility>
#include <iostream>
template <int N>
int func()
{ return N; }
template <int ... Is>
int func2 (int n, std::integer_sequence<int, Is...>)
{
int ret {};
if ( false == ( ... || (Is == n ? (ret = func<Is>(), true) : false) ) )
throw std::runtime_error{"no matching func()"};
return ret;
}
template <int I = 20>
int func3 (int n)
{ return func2(n, std::make_integer_sequence<int, I>{}); }
int main()
{
int n;
std::cin >> n;
std::cout << func3(n) << std::endl;
}
Starting from C++20, you can also use template lambdas, so you can also write
#include <utility>
#include <iostream>
template <int N>
int func()
{ return N; }
int main()
{
int n;
std::cin >> n;
std::cout << []<int ... Is>(int n, std::integer_sequence<int, Is...>)
{
int ret {};
if ( false == ( ... || (Is == n ? (ret = func<Is>(), true) : false) ) )
throw std::runtime_error{"no matching func()"};
return ret;
}(n, std::make_integer_sequence<int, 20>{}) << std::endl;
}

C++ templates to avoid long switches, while calling a function with different return types

I have many functions q1, q2, q3, etc., each with a different return type (int, int64_t, std::string, etc.).
I also have a print_result function that prints out their results (and the time they take to run, but trimmed here for simplicity):
template <typename T>
void print_result(T (*func)()) {
T res = func();
std::cout << res << std::endl;
}
I also have big switch statement to print the result for each of the functions:
switch (question_num) {
case 1: print_result(q1); break;
case 2: print_result(q2); break;
case 3: print_result(q3); break;
// ...
}
Objective: I would like to replace this switch statement with a template function, to avoid copying each line every time I add a new function.
I have tried to look at C++ template instantiation: Avoiding long switches, but I'm new to template metaprogramming, so not sure how to handle this exactly.
My current attempt that doesn't compile:
template <<int, typename> ...> struct FuncList {};
template <typename T>
bool handle_cases(int, T, FuncList<>) {
// default case
return false;
}
template <<int I, typename T> ...S>
bool handle_cases(int i, T (*func)(), FuncList<T, S...>) {
if (I != i) {
return handle_cases(i, func, FuncList<S...>());
}
print_result(func);
return true;
}
template <typename ...S>
bool handle_cases(int i, T (*func)()) {
return handle_cases(i, func, FuncList<S...>());
}
// ...
bool res = handle_cases<
<1, q1>, <2, q2>, <3, q3>
>(question_num);
// ...
My ideal way of using this template is shown at the last line there.
Note that the mappings from the function number to the function is provided there. The function numbers are fixed, i.e. q1 maps to the constant 1 and that won't change at runtime.
The compilation error (it might be rather basic but I really don't know much about metaprogramming):
error: expected unqualified-id before ‘<<’ token
17 | template <<int, typename> ...> struct FuncList {};
| ^~
If you can use c++17, here's a "simplified" version of #Klaus's approach. Instead of using a had-made recursive structure, you could use a c++17 fold-expression:
template<auto... Funcs, std::size_t... I>
bool select_case(std::size_t i, std::integer_sequence<std::size_t, I...>) {
return ([&]{ if(i == I) { print_result(Funcs); return true; } return false; }() || ... );
}
template<auto... Funcs>
struct FuncSwitch {
static bool Call(std::size_t i) {
return select_case<Funcs...>(i, std::make_index_sequence<sizeof...(Funcs)>());
}
};
The idea is to wrap each of Funcs in a lambda such that only the function corresponding to the index passed is called. Note that the || in the fold expression short-circuits.
Would be used like this:
float q0() { return 0.f; }
int q1() { return 1; }
std::string q2() { return "two"; }
int main() {
bool success = FuncSwitch<q0, q1, q2>::Call(1);
}
See here for a complete example.
I've got a different proposal:
Use an std::array instead of switch (or std::map if the switch cases are non-continuous, std::array has O(1) access time, std::map O(log(n)) and switch O(n).
Use std::function and std::bind to bind your functions you want to call to a functor object
use the index into the array to call the function
Use placeholders if you need to pass additional data
#include <iostream>
#include <functional>
template <typename T>
void print_result(T (*func)()) {
T res = func();
std::cout << res << std::endl;
}
int int_function() {
return 3;
}
double double_function() {
return 3.5;
}
std::array<std::function<void()>, 2> functions({
std::bind(print_result<int>, int_function),
std::bind(print_result<double>, double_function),
});
int main() {
functions[0]();
functions[1]();
return 0;
}
Output:
3
3.5
See: Why does std::function can implicit convert to a std::function which has more parameter?
Update:
With parameter passing:
#include <iostream>
#include <functional>
template <typename T>
void print_result(T (*func)(int), int value) {
T res = func(value);
std::cout << res << std::endl;
}
int int_function(int value) {
return 3 * value;
}
double double_function(int value) {
return 3.5 * value;
}
std::array<std::function<void(int)>, 2> functions({
std::bind(print_result<int>, int_function, std::placeholders::_1),
std::bind(print_result<double>, double_function, std::placeholders::_1),
});
int main() {
functions[0](10);
functions[1](11);
return 0;
}
Output:
30
38.5
You may like a version which do not need any kind of runtime containers, did not generate any objects in between and even do not generate a data table and generates very less code and is also easy to use:
// Example functions
int fint() { return 1; }
double fdouble() { return 2.2; }
std::string fstring() { return "Hallo"; }
// your templated result printer
template < typename T>
void print_result( T parm )
{
std::cout << "The result of call is " << parm << std::endl;
}
// lets create a type which is able to hold functions
template < auto ... FUNCS >
struct FUNC_CONTAINER
{
static constexpr unsigned int size = sizeof...(FUNCS);
};
// and generate a interface to switch
template < unsigned int, typename T >
struct Switch_Impl;
template < unsigned int IDX, auto HEAD, auto ... TAIL >
struct Switch_Impl< IDX, FUNC_CONTAINER<HEAD, TAIL...>>
{
static void Do( unsigned int idx )
{
if ( idx == IDX )
{
// Your function goes here
print_result(HEAD());
}
else
{
if constexpr ( sizeof...(TAIL))
{
Switch_Impl< IDX+1, FUNC_CONTAINER<TAIL...>>::Do(idx);
}
}
}
};
// a simple forwarder to simplify the interface
template < typename T>
struct Switch
{
static void Do(unsigned int idx )
{
Switch_Impl< 0, T >::Do( idx );
}
};
// and lets execute the stuff
int main()
{
using FUNCS = FUNC_CONTAINER< fint, fdouble, fstring >;
for ( unsigned int idx = 0; idx< FUNCS::size; idx++ )
{
Switch<FUNCS>::Do(idx);
}
}
Given you "current attempt"... it seems to me that you could write a handle_cases struct/class almost as follows
struct handle_cases
{
std::map<int, std::function<void()>> m;
template <typename ... F>
handle_cases (std::pair<int, F> const & ... p)
: m{ {p.first, [=]{ print_result(p.second); } } ... }
{ }
void operator() (int i)
{ m[i](); }
};
with a map between an integer and a lambda that call print_result with the function and an operator() that call the requested lambda, given the corresponding index.
You can create an object of the class as follows (unfortunately I don't see a way to avoid the std::make_pair()s)
handle_cases hc{ std::make_pair(10, q1),
std::make_pair(20, q2),
std::make_pair(30, q3),
std::make_pair(40, q4) };
and using it as follows
hc(30);
The following is a full compiling example
#include <functional>
#include <map>
#include <iostream>
template <typename T>
void print_result (T(*func)())
{
T res = func();
std::cout << res << std::endl;
}
struct handle_cases
{
std::map<int, std::function<void()>> m;
template <typename ... F>
handle_cases (std::pair<int, F> const & ... p)
: m{ {p.first, [=]{ print_result(p.second); } } ... }
{ }
void operator() (int i)
{ m[i](); }
};
char q1 () { return '1'; }
int q2 () { return 2; }
long q3 () { return 3l; }
long long q4 () { return 4ll; }
int main ()
{
handle_cases hc{ std::make_pair(10, q1),
std::make_pair(20, q2),
std::make_pair(30, q3),
std::make_pair(40, q4) };
hc(30);
}

Variadic template heterogeneous container

I need to implement some variadic template container class with heterogeneous elements, which allows to iterate by these elements. My first idea is make class with std::tuple member with variadic arguments, but getting elements from tuple by array-like manner (via loops) is impossible:
struct A {void prnt(){std::cout<<"A\n";} };
struct B {void prnt(){std::cout<<"B\n";} };
struct C {void prnt(){std::cout<<"C\n";} };
template<typename...Arg>
struct Prc
{
Prc() : NumElems(sizeof...(Arg)), mems(std::make_tuple(Arg()...)){}
int NumElems;
std::tuple<Arg...> mems;
void process()
{
for(int i=0; i<NumElems; ++i)
std::get<i>(mems).prnt();//It's forbidden: "i" must be a constant
}
};
int main()
{
Prc<A,B,C> obj;
obj.process();
}
Any ideas?
P.S. I don't want use boost heterogenous containers, like boost::variant or boost::any
Here's it done using indicies:
namespace detail
{
template <int... Is>
struct index { };
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template <int... Is>
struct gen_seq<0, Is...> : index<Is...> { };
}
template <typename...Args>
struct Prc
{
std::tuple<Args...> mems;
template <int... Is>
void process(detail::index<Is...>)
{
auto l = { (std::get<Is>(mems).prnt(), 0) ... };
}
void process()
{
process(detail::gen_seq<sizeof...(Args)>());
}
};
Here is some code that iterates a tuple:
struct A { void print () { clog << "A" << endl; } };
struct B { void print () { clog << "B" << endl; } };
struct C { void print () { clog << "C" << endl; } };
template<unsigned N>
struct iter
{
template<typename T>
static void f (T &t)
{
iter<N-1>::f (t);
get<N> (t).print ();
}
};
template<>
struct iter<0>
{
template<typename T>
static void f (T &t)
{
get<0> (t).print ();
}
};
And the calling code:
tuple <A,B,C> t;
iter<tuple_size<decltype(t)>::value-1>::f (t);
I think you could modify this to fit your needs. NumElements in your code is known at compile time, so I think you would remove that member altogether.

C++11: Compile Time Calculation of Array

Suppose I have some constexpr function f:
constexpr int f(int x) { ... }
And I have some const int N known at compile time:
Either
#define N ...;
or
const int N = ...;
as needed by your answer.
I want to have an int array X:
int X[N] = { f(0), f(1), f(2), ..., f(N-1) }
such that the function is evaluated at compile time, and the entries in X are calculated by the compiler and the results are placed in the static area of my application image exactly as if I had used integer literals in my X initializer list.
Is there some way I can write this? (For example with templates or macros and so on)
Best I have: (Thanks to Flexo)
#include <iostream>
#include <array>
using namespace std;
constexpr int N = 10;
constexpr int f(int x) { return x*2; }
typedef array<int, N> A;
template<int... i> constexpr A fs() { return A{{ f(i)... }}; }
template<int...> struct S;
template<int... i> struct S<0,i...>
{ static constexpr A gs() { return fs<0,i...>(); } };
template<int i, int... j> struct S<i,j...>
{ static constexpr A gs() { return S<i-1,i,j...>::gs(); } };
constexpr auto X = S<N-1>::gs();
int main()
{
cout << X[3] << endl;
}
There is a pure C++11 (no boost, no macros too) solution to this problem. Using the same trick as this answer we can build a sequence of numbers and unpack them to call f to construct a std::array:
#include <array>
#include <algorithm>
#include <iterator>
#include <iostream>
template<int ...>
struct seq { };
template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };
template<int ...S>
struct gens<0, S...> {
typedef seq<S...> type;
};
constexpr int f(int n) {
return n;
}
template <int N>
class array_thinger {
typedef typename gens<N>::type list;
template <int ...S>
static constexpr std::array<int,N> make_arr(seq<S...>) {
return std::array<int,N>{{f(S)...}};
}
public:
static constexpr std::array<int,N> arr = make_arr(list());
};
template <int N>
constexpr std::array<int,N> array_thinger<N>::arr;
int main() {
std::copy(begin(array_thinger<10>::arr), end(array_thinger<10>::arr),
std::ostream_iterator<int>(std::cout, "\n"));
}
(Tested with g++ 4.7)
You could skip std::array entirely with a bit more work, but I think in this instance it's cleaner and simpler to just use std::array.
You can also do this recursively:
#include <array>
#include <functional>
#include <algorithm>
#include <iterator>
#include <iostream>
constexpr int f(int n) {
return n;
}
template <int N, int ...Vals>
constexpr
typename std::enable_if<N==sizeof...(Vals),std::array<int, N>>::type
make() {
return std::array<int,N>{{Vals...}};
}
template <int N, int ...Vals>
constexpr
typename std::enable_if<N!=sizeof...(Vals), std::array<int,N>>::type
make() {
return make<N, Vals..., f(sizeof...(Vals))>();
}
int main() {
const auto arr = make<10>();
std::copy(begin(arr), end(arr), std::ostream_iterator<int>(std::cout, "\n"));
}
Which is arguably simpler.
Boost.Preprocessor can help you. The restriction, however, is that you have to use integral literal such as 10 instead of N (even be it compile-time constant):
#include <iostream>
#include <boost/preprocessor/repetition/enum.hpp>
#define VALUE(z, n, text) f(n)
//ideone doesn't support Boost for C++11, so it is C++03 example,
//so can't use constexpr in the function below
int f(int x) { return x * 10; }
int main() {
int const a[] = { BOOST_PP_ENUM(10, VALUE, ~) }; //N = 10
std::size_t const n = sizeof(a)/sizeof(int);
std::cout << "count = " << n << "\n";
for(std::size_t i = 0 ; i != n ; ++i )
std::cout << a[i] << "\n";
return 0;
}
Output (ideone):
count = 10
0
10
20
30
40
50
60
70
80
90
The macro in the following line:
int const a[] = { BOOST_PP_ENUM(10, VALUE, ~) };
expands to this:
int const a[] = {f(0), f(1), ... f(9)};
A more detail explanation is here:
BOOST_PP_ENUM
If you want the array to live in static memory, you could try this:
template<class T> struct id { typedef T type; };
template<int...> struct int_pack {};
template<int N, int...Tail> struct make_int_range
: make_int_range<N-1,N-1,Tail...> {};
template<int...Tail> struct make_int_range<0,Tail...>
: id<int_pack<Tail...>> {};
#include <array>
constexpr int f(int n) { return n*(n+1)/2; }
template<class Indices = typename make_int_range<10>::type>
struct my_lookup_table;
template<int...Indices>
struct my_lookup_table<int_pack<Indices...>>
{
static const int size = sizeof...(Indices);
typedef std::array<int,size> array_type;
static const array_type& get()
{
static const array_type arr = {{f(Indices)...}};
return arr;
}
};
#include <iostream>
int main()
{
auto& lut = my_lookup_table<>::get();
for (int i : lut)
std::cout << i << std::endl;
}
If you want a local copy of the array to work on, simply remove the ampersand.
There are quite a few great answers here. The question and tags specify c++11, but as a few years have passed, some (like myself) stumbling upon this question may be open to using c++14. If so, it is possible to do this very cleanly and concisely using std::integer_sequence; moreover, it can be used to instantiate much longer arrays, since the current "Best I Have" is limited by recursion depth.
constexpr std::size_t f(std::size_t x) { return x*x; } // A constexpr function
constexpr std::size_t N = 5; // Length of array
using TSequence = std::make_index_sequence<N>;
static_assert(std::is_same<TSequence, std::integer_sequence<std::size_t, 0, 1, 2, 3, 4>>::value,
"Make index sequence uses std::size_t and produces a parameter pack from [0,N)");
using TArray = std::array<std::size_t,N>;
// When you call this function with a specific std::integer_sequence,
// the parameter pack i... is used to deduce the the template parameter
// pack. Once this is known, this parameter pack is expanded in
// the body of the function, calling f(i) for each i in [0,N).
template<std::size_t...i>
constexpr TArray
get_array(std::integer_sequence<std::size_t,i...>)
{
return TArray{{ f(i)... }};
}
int main()
{
constexpr auto s = TSequence();
constexpr auto a = get_array(s);
for (const auto &i : a) std::cout << i << " "; // 0 1 4 9 16
return EXIT_SUCCESS;
}
I slightly extended the answer from Flexo and Andrew Tomazos so that the user can specify the computational range and the function to be evaluated.
#include <array>
#include <iostream>
#include <iomanip>
template<typename ComputePolicy, int min, int max, int ... expandedIndices>
struct ComputeEngine
{
static const int lengthOfArray = max - min + sizeof... (expandedIndices) + 1;
typedef std::array<typename ComputePolicy::ValueType, lengthOfArray> FactorArray;
static constexpr FactorArray compute( )
{
return ComputeEngine<ComputePolicy, min, max - 1, max, expandedIndices...>::compute( );
}
};
template<typename ComputePolicy, int min, int ... expandedIndices>
struct ComputeEngine<ComputePolicy, min, min, expandedIndices...>
{
static const int lengthOfArray = sizeof... (expandedIndices) + 1;
typedef std::array<typename ComputePolicy::ValueType, lengthOfArray> FactorArray;
static constexpr FactorArray compute( )
{
return FactorArray { { ComputePolicy::compute( min ), ComputePolicy::compute( expandedIndices )... } };
}
};
/// compute 1/j
struct ComputePolicy1
{
typedef double ValueType;
static constexpr ValueType compute( int i )
{
return i > 0 ? 1.0 / i : 0.0;
}
};
/// compute j^2
struct ComputePolicy2
{
typedef int ValueType;
static constexpr ValueType compute( int i )
{
return i * i;
}
};
constexpr auto factors1 = ComputeEngine<ComputePolicy1, 4, 7>::compute( );
constexpr auto factors2 = ComputeEngine<ComputePolicy2, 3, 9>::compute( );
int main( void )
{
using namespace std;
cout << "Values of factors1" << endl;
for ( int i = 0; i < factors1.size( ); ++i )
{
cout << setw( 4 ) << i << setw( 15 ) << factors1[i] << endl;
}
cout << "------------------------------------------" << endl;
cout << "Values of factors2" << endl;
for ( int i = 0; i < factors2.size( ); ++i )
{
cout << setw( 4 ) << i << setw( 15 ) << factors2[i] << endl;
}
return 0;
}
Here's a more concise answer where you explicitly declare the elements in the original sequence.
#include <array>
constexpr int f(int i) { return 2 * i; }
template <int... Ts>
struct sequence
{
using result = sequence<f(Ts)...>;
static std::array<int, sizeof...(Ts)> apply() { return {{Ts...}}; }
};
using v1 = sequence<1, 2, 3, 4>;
using v2 = typename v1::result;
int main()
{
auto x = v2::apply();
return 0;
}
How about this one?
#include <array>
#include <iostream>
constexpr int f(int i) { return 2 * i; }
template <int N, int... Ts>
struct t { using type = typename t<N - 1, Ts..., 101 - N>::type; };
template <int... Ts>
struct t<0u, Ts...>
{
using type = t<0u, Ts...>;
static std::array<int, sizeof...(Ts)> apply() { return {{f(Ts)...}}; }
};
int main()
{
using v = typename t<100>::type;
auto x = v::apply();
}
I don't think that's the best way to do this, but one can try somewhat like this:
#include <array>
#include <iostream>
#include <numbers>
constexpr auto pi{std::numbers::pi_v<long double>};
template <typename T>
struct fun
{
T v;
explicit constexpr fun(T a) : v{a * a} {}
};
template <size_t N, typename T, typename F>
struct pcl_arr
{
std::array<T, N> d;
explicit constexpr pcl_arr()
: d{}
{
for (size_t i{}; i < N; d[i] = !i ? 0. : F(pi + i).v, ++i);
}
};
int main()
{
using yummy = pcl_arr<10, long double, fun<long double>>;
constexpr yummy pies;
std::array cloned_pies{pies.d};
// long double comparison is unsafe
// it's just for the sake of example
static_assert(pies.d[0] == 0.);
for (const auto & pie : pies.d) { std::cout << pie << ' '; } std::cout << '\n';
for (const auto & pie : cloned_pies) { std::cout << pie << ' '; } std::cout << '\n';
return 0;
}
godbolt.org x86-x64 gcc 11.2 -Wall -O3 -std=c++20 output:
0 17.1528 26.436 37.7192 51.0023 66.2855 83.5687 102.852 124.135 147.418
0 17.1528 26.436 37.7192 51.0023 66.2855 83.5687 102.852 124.135 147.418