I'm using an array of pointer to function. I wrote the code like this since some of elements can not be expressed with function template.
我正在使用一个指向函数的指针数组。我写了这样的代码,因为有些元素不能用函数模板表示。
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>,
};
I've read about static table generation with variadic template, but those were about initializing the whole array.
我已经阅读了关于使用可变参数模板的静态表生成,但那些是关于初始化整个数组。
How can I simplify the code?
如何简化代码?
@ Actually It's a part of interrupt vector. Since it should be called directly, I cannot use template specialization such as
@实际上它是中断向量的一部分。由于它应该直接调用,我不能使用模板专业化,如
template <>
void Nth<0>() {
zero();
}
@@ Edited the code. I think that cout things were confusing.
@@编辑代码。我认为那些事情令人困惑。
3 个解决方案
#1
6
If you can change to use std::array
then something like this would work.
如果你可以改为使用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
1
I would recommended that you wrap your function around a class/struct so that you can take advantage of template specialization and declare your function as static inside of the class/struct
我建议你将你的函数包装在一个类/ struct中,这样你就可以利用模板特化并在类/ struct中将你的函数声明为static
#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
1
Following may help:
以下可能有所帮助
#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>());
#1
6
If you can change to use std::array
then something like this would work.
如果你可以改为使用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
1
I would recommended that you wrap your function around a class/struct so that you can take advantage of template specialization and declare your function as static inside of the class/struct
我建议你将你的函数包装在一个类/ struct中,这样你就可以利用模板特化并在类/ struct中将你的函数声明为static
#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
1
Following may help:
以下可能有所帮助
#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>());