C++ 模板指向引用类型的指针 tomfoolery
C++ templates pointer-to-reference-type tomfoolery
我有以下结构静态方法:
template<typename Edge1, typename Edge2, typename... Edges>
requires std::derived_from<Edge1, Edge> && std::derived_from<Edge2, Edge> && (std::derived_from<Edges, Edge> && ...)
[[nodiscard]] static constexpr bool are_multi_edges(const Edge1& edge1, const Edge2& edge2, const Edges&... edges)
{
// If only two edges were passed to the function, there's no need for any extra logic.
if constexpr (sizeof...(edges) == 0)
return edge1 == edge2;
// Problem here //
std::array<Edge, sizeof...(edges) + 1> array {edge2, edges...};
// Problem here //
for (auto& [from, to] : array)
if (from != edge1.from || to != edge1.to)
return false;
return true;
}// with "Edge" being the name of the actual struct housing the static method.
强调std::array<边缘,......>......
如果我按原样保留“Edge”,代码编译并工作正常,但我怀疑它会将 edge2
和 edges
复制到数组中,这是不必要的。我尝试用“Edge&”替换“Edge”以避免复制,但出现以下错误:
/usr/include/c++/10/array: In instantiation of ‘struct std::array<Edge<>&, 1>’:
/home/selamba/CLionProjects/graphs_lol/graph.h:41:43: required from ‘static constexpr bool Edge<VertexType>::are_multi_edges(const Edge1&, const Edge2&, const Edges& ...) [with Edge1 = Edge<>; Edge2 = Edge<>; Edges = {}; VertexType = char]’
/home/selamba/CLionProjects/graphs_lol/main.cpp:13:51: required from here
/usr/include/c++/10/array:97:35: error: forming pointer to reference type ‘std::array<Edge<>&, 1>::value_type’ {aka ‘Edge<>&’}
97 | typedef value_type* pointer;
| ^~~~~~~
主要内容是“形成指向引用类型的指针”。这只是错误消息的顶部,因为实际消息非常庞大。所有错误实际上都在“数组”头文件中(usr/include/c++/10/array)。
是否可以在我的用例中形成一个 std::array 边缘引用?如果是这样,我该怎么做?
使用标准参考包装器。引用不规则,不适合在 std 容器中使用。
引用包装器介于引用和指针之间,但足够规则以成为容器内容。
我有以下结构静态方法:
template<typename Edge1, typename Edge2, typename... Edges>
requires std::derived_from<Edge1, Edge> && std::derived_from<Edge2, Edge> && (std::derived_from<Edges, Edge> && ...)
[[nodiscard]] static constexpr bool are_multi_edges(const Edge1& edge1, const Edge2& edge2, const Edges&... edges)
{
// If only two edges were passed to the function, there's no need for any extra logic.
if constexpr (sizeof...(edges) == 0)
return edge1 == edge2;
// Problem here //
std::array<Edge, sizeof...(edges) + 1> array {edge2, edges...};
// Problem here //
for (auto& [from, to] : array)
if (from != edge1.from || to != edge1.to)
return false;
return true;
}// with "Edge" being the name of the actual struct housing the static method.
强调std::array<边缘,......>......
如果我按原样保留“Edge”,代码编译并工作正常,但我怀疑它会将 edge2
和 edges
复制到数组中,这是不必要的。我尝试用“Edge&”替换“Edge”以避免复制,但出现以下错误:
/usr/include/c++/10/array: In instantiation of ‘struct std::array<Edge<>&, 1>’:
/home/selamba/CLionProjects/graphs_lol/graph.h:41:43: required from ‘static constexpr bool Edge<VertexType>::are_multi_edges(const Edge1&, const Edge2&, const Edges& ...) [with Edge1 = Edge<>; Edge2 = Edge<>; Edges = {}; VertexType = char]’
/home/selamba/CLionProjects/graphs_lol/main.cpp:13:51: required from here
/usr/include/c++/10/array:97:35: error: forming pointer to reference type ‘std::array<Edge<>&, 1>::value_type’ {aka ‘Edge<>&’}
97 | typedef value_type* pointer;
| ^~~~~~~
主要内容是“形成指向引用类型的指针”。这只是错误消息的顶部,因为实际消息非常庞大。所有错误实际上都在“数组”头文件中(usr/include/c++/10/array)。
是否可以在我的用例中形成一个 std::array 边缘引用?如果是这样,我该怎么做?
使用标准参考包装器。引用不规则,不适合在 std 容器中使用。
引用包装器介于引用和指针之间,但足够规则以成为容器内容。