Nis*_*ann 9 c++ templates metaprogramming variadic-templates c++17
我目前正在研究使用 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 {};
};
Run Code Online (Sandbox Code Playgroud)
使用上面的树的定义,我们可以定义一组电路组件如下:
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;
}
};
Run Code Online (Sandbox Code Playgroud)
接下来,利用上面的组件,我们可以这样表达一个具体的电路:
enum { R0, R1, C0, C1 };
using Circuit =
Tree<
Parallel<
Series<
Resistor<R0>,
Capacitor<C0>
>, // Series
Series<
Resistor<R0>,
Capacitor<C1>
> // Series
> // Parallel
>; // Tree
Run Code Online (Sandbox Code Playgroud)
...其中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;
}
Run Code Online (Sandbox Code Playgroud)
我无法理解的是如何实现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)), ...);
}
};
Run Code Online (Sandbox Code Playgroud)
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));
}
};
Run Code Online (Sandbox Code Playgroud)
在研究了有关 C++ 可变参数模板的各种文章后,我设法修补了该问题的解决方案。
首先,为了实现,for_each_child我们使用以下辅助函数,该函数作为在编译时展开的for 循环:
template <auto from, auto to, typename Lambda>
static inline constexpr void for_constexpr(Lambda && lambda)
{
if constexpr (from < to)
{
constexpr auto i = std::integral_constant<decltype(from), from>();
lambda(i);
for_constexpr<from + 1, to>(lambda);
}
}
Run Code Online (Sandbox Code Playgroud)
通过使用这个辅助函数,我们可以实现for_each_child如下:
template <typename ... Children> struct Branch
{
static constexpr auto children_count = sizeof ... (Children);
template <typename Lambda> constexpr void for_each_child(Lambda && lambda)
{
for_constexpr<0, children_count>([lambda, this](auto i)
{
lambda(std::get<i>(m_children));
});
}
std::tuple<Children ...> m_children {};
};
Run Code Online (Sandbox Code Playgroud)
接下来,为了实现get_leaf,我们使用了一堆不同的辅助函数。正如Caleth 所建议的,我们可以将问题分为两步。首先,我们计算从根到所需叶子的路径;之后,我们可以沿着这条路径从树上提取叶子。
路径可以表示为索引序列,如下所示:
template <auto ...indices> using Path = std::index_sequence<indices...>;
Run Code Online (Sandbox Code Playgroud)
我们需要的第一个辅助函数检查节点是否具有带有给定标签的叶子:
template <auto tag, class Node> struct has_path
{
static constexpr
std::true_type
match(const Leaf<tag>);
template <class ...Children> static constexpr
typename std::enable_if<
(has_path<tag, Children>::type::value || ...),
std::true_type
>::type
match(const Branch<Children...>);
static constexpr
std::false_type
match(...);
using type = decltype(match(std::declval<Node>()));
};
Run Code Online (Sandbox Code Playgroud)
我们只需在节点上进行模式匹配即可。如果它是叶子,我们必须确保它具有正确的标签。而且,如果它是一个分支,我们需要确保其中一个子节点具有带有标签的叶子。
下一个辅助函数有点复杂:
template <auto tag, class Node, auto ...indices> struct find_path
{
template <auto index, class Child, class ...Children> struct search_children
{
static constexpr auto fold()
{
if constexpr(has_path<tag, Child>::type::value)
{
return typename find_path<tag, Child, indices..., index>::type();
}
else
{
return typename search_children<index + 1, Children...>::type();
}
}
using type = decltype(fold());
};
static constexpr
Path<indices...>
match(const Leaf<tag>);
template <class ...Children> static constexpr
typename search_children<0, Children...>::type
match(const Branch<Children...>);
using type = decltype(match(std::declval<Node>()));
};
Run Code Online (Sandbox Code Playgroud)
我们在模板参数中累积路径indices。如果我们正在研究的节点(通过模板参数Node)是叶子,我们检查它是否具有正确的标签,如果是,则返回累积路径。相反,如果该节点是一个分支,我们必须使用辅助函数来search_children迭代分支中的所有子节点。对于每个孩子,我们首先检查该孩子是否具有带有给定标签的叶子。如果是这样,我们将当前索引(由模板参数给出index)附加到累积路径并递归find_path调用该子项。如果子节点没有具有给定标签的叶子,我们将尝试下一个子节点,依此类推。
最后,我们定义一个辅助函数,可以提取给定路径的叶子:
template <class Node>
static inline constexpr auto &
get(Node & leaf, Path<> path)
{
return leaf;
}
template <auto index, auto ...indices, class Node>
static inline constexpr auto &
get(Node & branch, Path<index, indices...> path)
{
auto & child = std::get<index>(branch.m_children);
return get(child, Path<indices...>());
}
Run Code Online (Sandbox Code Playgroud)
使用find_pathandget我们可以实现get_leaf如下:
template <typename Root> struct Tree
{
template <auto tag> constexpr auto & get_leaf()
{
constexpr auto path = typename implementation::find_path<tag, Root>::type {};
return implementation::get(root, path);
}
Root root;
};
Run Code Online (Sandbox Code Playgroud)
以下是godbolt.org的链接,演示了代码可以使用 Clang 进行编译并按预期工作:
| 归档时间: |
|
| 查看次数: |
454 次 |
| 最近记录: |