Passing a constexpr function to use at compile time - c++

I'm trying to make a simple way to generate std::array's at compile time. It's been a struggle so far to figure out a good way to pass a constexpr function in at compile time. The workaround I have found so far is like this.
#include <iostream>
#include <array>
namespace a {
constexpr int f(const int & i) { return i * i * i;}
#include "generate_with_function.hpp"
}
int main()
{
auto arr = a::generator<false,10,0>::array;
for (auto i : arr) {
std::cout << i << " ";
}
return 0;
}
this basically assumes you will define a function called f and I wrapped it in a namespace incase I wanted to do a different one. I wanted to know if there is a more clever way to pass in the function and use it at compile time. Also here is the code that makes the list.
template <bool B, size_t Count,int ... Nums>
struct generator;
template <size_t Count>
struct generator<false,Count,0>
{
constexpr static std::array<int,Count> array
= generator<false,Count,1,f(0)>::array;
};
template <size_t Count, int Current, int ... Results>
struct generator<false,Count,Current, Results...>
{
constexpr static std::array<int,Count> array
= generator<Current+1==Count,Count,Current+1,f(Current), Results...>::array;
};
template <size_t Count, int Current, int ... Results>
struct generator<true,Count,Current,Results...>
{
constexpr static std::array<int,Count> array{{Results...}};
};
and before you ask no I don't actually have a real need for this.
As noted by #us2012 I should be specific about what I would rather have.
nowrapping in namespace
having to write the function but not actually passing it anywhere
and not requiring the function to be named f

You can actually use the function as a template parameter, here called Gen:
template <bool B, size_t Count, int Current, int Gen(size_t), int ... Nums>
struct generator;
template <size_t Count, int Current, int Gen(size_t), int ... Results>
struct generator<false,Count,Current, Gen, Results...>
{
constexpr static std::array<int,Count> array
generator<Current+1==Count,Count,Current+1,Gen,
Gen(Current), Results...>::array;
};
template <size_t Count, int Current, int Gen(size_t), int ... Results>
struct generator<true,Count,Current,Gen,Results...>
{
constexpr static std::array<int,Count> array{{Results...}};
};
With this it actually works to pass a constexpr function to the template (if the type matches exactly):
// helper to hide the "internal" template parameters
template <size_t Count, int Gen(size_t)>
struct gen {
constexpr static std::array<int, Count> array = generator<false, Count, 0, Gen>::array;
};
constexpr int f(size_t i) { return i * i * i; }
int main()
{
auto arr = gen<10,f>::array;
for (auto i : arr) {
std::cout << i << " ";
}
return 0;
}
You can also switch around the Gen(Current) and Results... parameters to get the values at the "correct" array indexes.

Related

I changed the order of the template struct in C++ and got an error

this code works.
#include <iostream>
template <int N>
struct Factorial {
static const int result = N * Factorial<N - 1>::result;
};
template <>
struct Factorial<1> {
static const int result = 1;
};
int main() {
std::cout << Factorial<6>::result << std::endl;
return 0;
}
but, this code doesn't work.
#include <iostream>
template <>
struct Factorial<1> {
static const int result = 1;
};
template <int N>
struct Factorial {
static const int result = N * Factorial<N - 1>::result;
};
int main() {
std::cout << Factorial<6>::result << std::endl;
return 0;
}
the error list is...
enter image description here
Since Factorial needs to reference it, I think Factorial<1> should be on top.
However, an error occurred that was not what I expected.
template <> struct Factorial<1> is template specialization of template <int N> struct Factorial. The template itself must come before its specialization.
When you write Factorial<N - 1>, you are using template <int N>. The specialization is resolved later once the value of N is known.
You may compare this logic to a function call: Function declaration void function(int); must come before function(1);, as the function call uses the function, not the other way around.

Represents empty type in C++ class template

consider the following example of a compile-time "vector".
#include <iostream>
template <int n, int...ns>
struct static_vector {
static constexpr int value = n;
static_vector<ns...> rest;
};
template <int n>
struct static_vector<n> {
static constexpr int value = n;
void* rest;
};
template <int n, class sv>
constexpr int access_nth() {
static_assert(n >= 0, "vector size out of bound");
if constexpr(n == 0) {
return sv::value;
} else {
static_assert(!std::is_same_v<decltype(sv::rest), void *>, "vector size out of bound");
return access_nth<n-1, decltype(sv::rest)>();
}
}
int main()
{
constexpr auto a = static_vector<12, 23, 34, 45>();
constexpr int nth = access_nth<5, decltype(a)>();
std::cout << nth << std::endl;
}
I am mostly satisfied with what we can do now: define a vector and then get the nth element out of it. The one thing I found that not satisfying is this: I have to use void * as a dummy in the base case (where the vector only holds one element and no tail...)
I tried to have a specialisation like this:
template <>
struct static_vector<> {
}
to represent the empty vector. But it seems that the compiler always rejects this definition with the following error:
<source>:16:8: error: too few template arguments for class template 'static_vector'
struct static_vector<> {
^
What should I do here so I can have an empty vector?
Thanks a lot.
But why recursion ?
You tagged C++17 so you can use template folding, so... what about as follows ?
#include <iostream>
template <int ... Is>
struct static_vector
{
template <std::size_t N>
int get () const
{
static_assert( N < sizeof...(Is), "index out of bound" );
std::size_t i{};
int ret;
( ... , (N == i++ ? ret = Is : 0) );
return ret;
}
};
int main()
{
constexpr auto a = static_vector<12, 23, 34, 45>();
std::cout << a.get<3u>() << std::endl;
}
Specializations must conform to the base template declaration. Since at least one int is required by the base template, this does not compile.
You can make this work by declaring the template to take any number of int arguments, then specializing every case that takes one or more arguments. The base declaration is then the empty case:
template <int...>
struct static_vector {
// Instantiated only for the no-argument case
};
template <int n>
struct static_vector<n> {
// One-argument specialization
};
template <int n, int... ns>
struct static_vector<n, ns...> {
// Two-or-more-argument specialization
};

C++ constexpr values for types

I want to be able to create switch statements over a type's ID. I've found a mechanism that could give a unique ID for different types. It's very simple:
template <typename T>
struct type {
static void id() { }
};
template <typename T>
constexpr const size_t type_id() {
return reinterpret_cast<size_t>(&type<T>::id);
}
I thought this would evaluate to a constant that I could use as cases for the switch. But I get an error that the case expression is not a constant when I do the following:
int main(void) {
size_t a = type_id<int>();
switch (a) {
case type_id<int>():
break;
}
return 0;
}
Why is it not a constant? How could I achieve this effect?
Edit:
Can I do something like this without the reinterpret_cast then?
I'm not sure it's a good idea but... just for fun... using the constexpr counter, suggested in this page, you should be able to substitute the value of the pointer.
The following is a (I repeat: just for fun) full experiment
#include <iostream>
template <int N>
struct flag
{ friend constexpr int adl_flag (flag<N>); };
template <int N>
struct writer
{
friend constexpr int adl_flag (flag<N>)
{ return N; }
static constexpr int value { N };
};
template <int N, int = adl_flag (flag<N> {})>
int constexpr reader (int, flag<N>)
{ return N; }
template <int N>
int constexpr reader (float, flag<N>, int R = reader (0, flag<N-1> {}))
{ return R; }
int constexpr reader (float, flag<0>)
{ return 0; }
template <int N = 1>
int constexpr next (int R = writer<reader (0, flag<32> {}) + N>::value)
{ return R; }
template <typename T>
struct type
{
static constexpr int id { next() };
constexpr static int type_id ()
{ return id; }
};
void printType (int idT )
{
switch ( idT )
{
case type<int>::type_id():
std::cout << "- int type" << std::endl;
break;
case type<long>::id:
std::cout << "- long type" << std::endl;
break;
default:
std::cout << "- another type" << std::endl;
break;
}
}
int main ()
{
int ii { type<int>::id };
int il { type<long>::type_id() };
printType(ii);
printType(il);
}
I would like to suggest another approach which involves constexpr functions and macros (eeeewww...):
// Some naive text hashing function
template <std::size_t SIZE>
constexpr std::size_t hash(const char (&type_name)[SIZE])
{
std::size_t result{0xf};
for (const auto &c : type_name)
{
result <<= 1;
result |= c;
}
return result;
}
First we create a constexpr function able to transform a string literal into a number, this is my approach but you can choose anoter function as long as it is constexpr, then we create a macro which stringify the given parameter using the #:
#define TYPE_ID(X) hash(#X)
And now, we can use it:
int main(void) {
size_t a = TYPE_ID(int);
switch (a) {
case TYPE_ID(int):
break;
}
return 0;
}
Pros:
Pretty straightforward.
Tiny amount of code.
Cons:
Macros.
Accepts any value, included nonsense: TYPE_ID(I LOVE BACON) is valid.
Yields different result for TYPE_ID(size_t) and TYPE_ID(unsigned long) even if they might be the same type.
constexpr functions can not use reinterpret_cast in any shape or form. Some more formal reading can be found at http://en.cppreference.com/w/cpp/language/constant_expression
This may solve your problem:
#include <tuple>
//Index from http://stackoverflow.com/a/18063608/3484570
template <class T, class Tuple>
struct Index;
template <class T, class... Types>
struct Index<T, std::tuple<T, Types...>> {
static const std::size_t value = 0;
};
template <class T, class U, class... Types>
struct Index<T, std::tuple<U, Types...>> {
static const std::size_t value = 1 + Index<T, std::tuple<Types...>>::value;
};
template <class T>
constexpr std::size_t type_id() {
//need to add every type in this tuple:
return Index<T, std::tuple<int, double, char>>::value;
}
int main() {
size_t a = type_id<int>();
switch (a) {
case type_id<int>():
break;
}
}
The good news is that you get a type_id<T>() that you can use in a constexpr context such as in a case like you wanted.
The bad news is that you need to list all supported types.
In practice you might get used to the error message that occurs when you ask for the type_id of an unsupported type and just add it and eventually add all relevant types.

Initialise static const int with static function

I have a template class with some integers as arguments. One static const integer (call it Length) of this class needs to be calculated based on the arguments. The calculation does need a loop (as far as I know) so a simple expression won't help.
static int setLength()
{
int length = 1;
while (length <= someTemplateArgument)
{
length = length << 1;
}
return length;
}
The returned length should be used to init Length. Lengthis used as a fixed length of an array so I need it to be constant.
Is there a solution for this issue? I know that constexp could help but I can't use C11 or later.
Using metaprogramming. Implementation of C++11 enable_if taken from cppreference.com
#include <iostream>
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
template <int length, int arg, typename = void>
struct length_impl
{
static const int value = length_impl<(length << 1), arg>::value;
};
template <int length, int arg>
struct length_impl<length, arg, typename enable_if<(length > arg)>::type>
{
static const int value = length ;
};
template <int arg>
struct length_holder
{
static const int value = length_impl<1, arg>::value;
};
template<int n>
struct constexpr_checker
{
static const int value = n;
};
int main()
{
std::cout << constexpr_checker< length_holder<20>::value >::value;
}

Variadic templates with constants

I'm doing some experimenting with metaprogramming and variadic templates, and I'm having trouble with some confusing behaviour. I've stripped it down to a minimum working example, but essentially I want to keep track of how many recursive calls I'm making. I want to do this by making the first template parameter an integer, while the second template parameter is a variadic list. In simplest form it looks like this:
template<typename... List>
struct initial_call{
static const int val = next_call<0, List...>::val;
};
template<int D, typename... List>
struct next_call {
static const int val = D;
};
So ignoring the fact that this code is pointless, it doesn't compile on VS2013, claiming "unexpected type 'List", on the line inside the definition of initial_call. Without the integer in front, it works fine. So is there a way to combine variadic templates with integer template parameters?
You might want something like this (counting the number of types):
#include <iostream>
// More than one type: Increment size and consume.
template<size_t N, typename T, typename... List>
struct calculate_list_size {
static const size_t value = calculate_list_size<N + 1, List...>::value;
};
// Last type: Increment size and terminate.
template<size_t N, typename T>
struct calculate_list_size<N, T> {
static const size_t value = N + 1;
};
// Forward to calculate_list_size.
template<typename... List>
struct list_size {
static const size_t value = calculate_list_size<0, List...>::value;
};
// Empty list
template<>
struct list_size<> {
static const size_t value = 0;
};
int main()
{
std::cout << list_size<char, short, int>::value << '\n';
std::cout << list_size<>::value << '\n';
}