我目前正在研究使用 C++ (C++17) 可变参数模板来生成高效、实时的电路仿真。
我的目标是利用可变参数模板来定义可以在编译时遍历的树。为了定义这样的树,我使用了以下三个结构:
template <auto Tag> struct Leaf
{
static constexpr auto tag = Tag;
};
template <typename ... Children> struct Branch
{
static constexpr auto child_count = sizeof ... (Children);
template <typename Lambda> constexpr void for_each_child(Lambda && lambda)
{
// TODO: Execute 'lambda' on each child.
}
std::tuple<Children ...> m_children {};
};
template <typename Root> struct Tree
{
template <auto Tag> constexpr auto & get_leaf()
{
// TODO: Traverse the tree and find the leaf with tag 'Tag'.
// If there's no leaf with tag 'Tag' the program shouldn't compile.
}
Root root {};
};
使用上面的树的定义,我们可以定义一组电路组件如下:template <auto Tag> struct Resistor : Leaf<Tag>
{
float resistance() { return m_resistance; }
float m_resistance {};
};
template <auto Tag> struct Capacitor : Leaf<Tag>
{
float resistance() { return 0.0f; }
float m_capacitance {};
};
template <typename ... Children> struct Series : Branch<Children ...>
{
using Branch<Children ...>::for_each_child;
float resistance()
{
float acc = 0.0f;
for_each_child([&acc](auto child) { acc += child.resistance(); });
return acc;
}
};
template <typename ... Children> struct Parallel : Branch<Children ...>
{
using Branch<Children ...>::for_each_child;
float resistance()
{
float acc = 0.0f;
for_each_child([&acc](auto child) { acc += 1.0f / child.resistance(); });
return 1.0f / acc;
}
};
接下来,利用上面的组件,我们可以这样表达一个具体的电路:enum { R0, R1, C0, C1 };
using Circuit =
Tree<
Parallel<
Series<
Resistor<R0>,
Capacitor<C0>
>, // Series
Series<
Resistor<R0>,
Capacitor<C1>
> // Series
> // Parallel
>; // Tree
...其中 R0、R1、C0 和 C1 是我们在编译时用于访问组件的标记。例如。一个非常基本的用例可能如下:int main()
{
Circuit circuit {};
circuit.get_leaf<R0>().m_resistance = 5.0E+3f;
circuit.get_leaf<C0>().m_capacitance = 10.0E-3f;
circuit.get_leaf<R1>().m_resistance = 5.0E+6f;
circuit.get_leaf<C1>().m_capacitance = 10.0E-6f;
std::cout << circuit.root.resistance() << std::endl;
}
我无法理解的是如何实现 for_each_child 和 get_leaf 函数。我尝试过使用 if-constexpr 语句和模板结构的不同方法,但没有找到好的解决方案。可变参数模板很有趣,但同时也令人生畏。任何帮助将不胜感激。
最佳答案
for_each_child
与 std::index_sequence
相当简单。
template <typename ... Children> struct Branch
{
using indexes = std::index_sequence_for<Children...>;
static constexpr auto child_count = sizeof... (Children);
template <typename Lambda> constexpr void for_each_child(Lambda && lambda)
{
for_each_child_impl(std::forward<Lambda>(lambda), indexes{});
}
std::tuple<Children ...> m_children {};
private:
template <typename Lambda, std::size_t... Is> constexpr void for_each_child_impl(Lambda && lambda, std::index_sequence<Is...>)
{
(lambda(std::get<Is>(m_children)), ...);
}
};
get_leaf
稍微有点棘手。首先,我们确定到达所需叶子的路径是什么,然后我们遵循 root
的路径。template <std::size_t I, typename>
struct index_sequence_cat;
template <std::size_t I, std::size_t... Is>
struct index_sequence_cat<I, std::index_sequence<Is...>> {
using type = std::index_sequence<I, Is...>;
};
template <std::size_t I, typename Ix>
using index_sequence_cat_t = typename index_sequence_cat<I, Ix>::type;
template<typename, auto Tag, typename, std::size_t... Is>
struct leaf_index {};
template<auto Tag, typename T, std::size_t... Is>
using leaf_index_i = typename leaf_index<void, Tag, T, Is...>::index;
template<auto Tag, std::size_t I>
struct leaf_index<void, Tag, Leaf<Tag>, I> {
using index = std::index_sequence<I>;
};
template<typename, auto, std::size_t, typename...>
struct branch_index {};
template<auto Tag, std::size_t I, typename... Args>
using branch_index_i = typename branch_index<void, Tag, I, Args...>::index;
template<auto Tag, std::size_t I, typename First, typename... Args>
struct branch_index<std::void_t<leaf_index_i<Tag, First, I>>, Tag, I, First, Args...> {
using index = leaf_index_i<Tag, First, I>;
};
template<auto Tag, std::size_t I, typename First, typename... Args>
struct branch_index<std::void_t<branch_index_i<Tag, I + 1, Args...>>, Tag, I, First, Args...> {
using index = branch_index_i<Tag, I + 1, Args...>;
};
template<auto Tag, typename... Children, std::size_t I>
struct leaf_index<void, Tag, Branch<Children...>, I> {
using index = index_sequence_cat_t<I, branch_index_i<Tag, 0, Children...>>;
};
template<auto Tag, typename... Children>
struct leaf_index<std::void_t<branch_index_i<Tag, 0, Children...>>, Tag, Branch<Children...>> {
using index = branch_index_i<Tag, 0, Children...>;
};
template <typename Root> struct Tree
{
template <auto Tag> constexpr auto & get_leaf()
{
return get_leaf(leaf_index<Tag, root>{});
}
Root root {};
private:
template <std::size_t... Is>
auto & get_leaf(std::index_sequence<Is...>)
{
return get_leaf<Is...>(root);
}
template<std::size_t I, typename T>
auto& get_leaf(T & branch)
{
return std::get<I>(branch.m_children);
}
template<std::size_t I, std::size_t J, std::size_t... Is, typename T>
auto& get_leaf(T & branch)
{
return get_leaf<J, Is...>(std::get<I>(branch.m_children));
}
};
关于c++ - 在编译时使用 C++17 可变模板遍历树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68115753/