Как я могу инициализировать массив во время компиляции с некоторыми элементами, указанными вручную?
Я использую массив указателя на функцию. Я написал такой код, так как некоторые элементы не могут быть выражены с помощью шаблона функции.
extern void zero(); // isr 0 is defined somewhere else
void one() {
// isr 1
}
template <std::size_t N>
void Nth() {
// isr N
}
using func = void (*)();
constexpr func interrupt_vector[256] = {
&zero,
&one,
&Nth<2>,
&Nth<3>,
...
&Nth<254>,
&Nth<255>,
};
Я читал о создании статической таблицы с вариационным шаблоном, но это было о инициализации всего массива.
Как я могу упростить код?
@На самом деле это часть вектора прерывания. Поскольку он должен быть вызван напрямую, я не могу использовать специализированную специализацию, например
template <>
void Nth<0>() {
zero();
}
@@Отредактированный код. Я думаю, что cout все путают.
Ответы
Ответ 1
Если вы можете изменить использование std::array
, тогда что-то вроде этого будет работать.
using func = void (*)();
template<int...>
struct index_sequence { };
template<int From, int N, int... Is>
struct make_index_sequence_from : make_index_sequence_from<From, N - 1, N - 1, Is...> { };
template<int From, int... Is>
struct make_index_sequence_from<From, From, Is...> : index_sequence<Is...> { };
template<int... Is> constexpr
std::array<func, 256> make_interrupt_vector_array(index_sequence<Is...>)
{
return {{zero, one, Nth<Is>...}};
}
constexpr
std::array<func, 256> make_interrupt_vector_array()
{
return make_interrupt_vector_array(make_index_sequence_from<2, 256>());
}
constexpr auto interrupt_vector = make_interrupt_vector_array();
Ответ 2
Я бы рекомендовал вам обернуть вашу функцию вокруг класса/структуры, чтобы вы могли воспользоваться специализацией шаблона и объявить свою функцию статичной внутри класса/структуры
#include <iostream>
template <std::size_t N>
struct Nth
{
static void print()
{
std::cout << N << "!!" << std::endl;
}
};
template <>
struct Nth<0>
{
static void print()
{
std::cout << "Zero!!" << std::endl;
}
};
template <>
struct Nth<1>
{
static void print()
{
std::cout << "One!!" << std::endl;
}
};
int main()
{
Nth<0>::print();
Nth<1>::print();
Nth<2>::print();
}
Ответ 3
Следующее может помочь:
#if 1 // Not in C++11
#include <cstdint>
template <std::size_t ...> struct index_sequence {};
template <std::size_t N, std::size_t ...Is>
struct make_index_sequence : make_index_sequence <N - 1, N - 1, Is...> {};
template <std::size_t ... Is>
struct make_index_sequence<0, Is...> : index_sequence<Is...> {};
#endif // make_index_sequence
using func = void (*)();
namespace detail
{
// general case
template <std::size_t N>
struct FuncPtr { static constexpr func value = &Nth<N>; };
// Some specializations // not necessary at the beginning
template <>
struct FuncPtr<0u> { static constexpr func value = &zero; };
template <>
struct FuncPtr<1u> { static constexpr func value = &one; };
// Function to create the array:
template <std::size_t ... Is>
constexpr std::array<func, sizeof...(Is)>
FuncPtrArray(index_sequence<Is...>)
{
return std::array<func, sizeof...(Is)>{{FuncPtr<Is>::value...}};
}
} // namespace detail
constexpr std::array<func, 256> interrupt_vector =
detail::FuncPtrArray(make_index_sequence<256>());