C++ constexpr链表-从const X*到X的转换无效*
下面是我创建一个简单的constexpr链表的尝试-C++ constexpr链表-从const X*到X的转换无效*,c++,data-structures,c++14,constexpr,c++17,C++,Data Structures,C++14,Constexpr,C++17,下面是我创建一个简单的constexpr链表的尝试- struct Node { constexpr Node(const int n, Node const* next = nullptr) : value(n), next(next) {} constexpr Node push(const int n) const { return Node(n, this); } int value; Node const* next; }; cons
struct Node
{
constexpr Node(const int n, Node const* next = nullptr)
: value(n), next(next) {}
constexpr Node push(const int n) const { return Node(n, this); }
int value;
Node const* next;
};
constexpr auto getSum(Node n) {
int sum = 0;
Node *current = &n;
while(current != nullptr) {
sum += current->value;
current = current->next;
}
return sum;
}
int main() {
constexpr Node a(0);
a.push(1);
a.push(22);
constexpr auto result = getSum(a);
return result;
}
编译此程序时,显示以下错误
prog.cc: In function 'constexpr auto getSum(Node)':
prog.cc:16:28: error: invalid conversion from 'const Node*' to 'Node*' [-fpermissive]
current = current->next;
~~~~~~~~~^~~~
prog.cc: In function 'int main()':
prog.cc:25:35: in constexpr expansion of 'getSum(a)'
prog.cc:16:28: error: conversion of 'const Node*' null pointer to 'Node*' is not a constant expression
我应该如何着手解决这个问题并生成这样的链表?这里是为了在线查看执行情况。正如@hg\u git在你文章的评论中指出的那样,
constexpr
链接列表是不可能的
我清理了你的代码,发现了一个有用的错误
#include <iostream>
struct Node
{
constexpr Node(const int n, Node * next = nullptr)
: value(n), next(next) {}
constexpr Node push(const int n) { return Node(n, this); }
int value;
Node * next;
};
constexpr auto getSum(Node n) {
int sum = 0;
Node *current = &n;
while(current != nullptr) {
sum += current->value;
current = current->next;
}
return sum;
}
int main() {
constexpr Node a(0);
a.push(1);
a.push(22);
constexpr auto result = getSum(a);
return result;
}
如您所见,即使关键字const
不存在,const参数仍然存在一些问题。这是因为constexpr是在编译时计算的。从而使它们在运行时不可变
链表可以在运行时更改;如果根据示例添加或删除节点。
因此,在这种情况下,constexpr
不是正确的选择
编辑:
这是您的一段代码,从
constexpr
中清除。我添加了一些注释,如果您不懂一行或一个函数,请随意询问。正如@hg\u git在您的帖子的注释中指出的那样,constexpr
链接列表是不可能的
我清理了你的代码,发现了一个有用的错误
#include <iostream>
struct Node
{
constexpr Node(const int n, Node * next = nullptr)
: value(n), next(next) {}
constexpr Node push(const int n) { return Node(n, this); }
int value;
Node * next;
};
constexpr auto getSum(Node n) {
int sum = 0;
Node *current = &n;
while(current != nullptr) {
sum += current->value;
current = current->next;
}
return sum;
}
int main() {
constexpr Node a(0);
a.push(1);
a.push(22);
constexpr auto result = getSum(a);
return result;
}
如您所见,即使关键字const
不存在,const参数仍然存在一些问题。这是因为constexpr是在编译时计算的。从而使它们在运行时不可变
链表可以在运行时更改;如果根据示例添加或删除节点。
因此,在这种情况下,constexpr
不是正确的选择
编辑:
这是您的一段代码,从
constexpr
中清除。我添加了一些注释,如果您不理解一行或一个函数,请随意询问。立即出现的错误很容易修复:
Node const *current = &n;
// ^^^^^
投诉是current=current->next
正在将一个节点常量*
分配给一个节点*
,所以不要这样做
这样做会使程序编译但打印0
,因为push
都不会调用modifieda
。您也不能将push
的结果存储为constexpr
,因为自动局部变量a
的地址不是常量表达式
但是,您可以形成临时节点的链接列表并立即使用它:
constexpr auto result = getSum(a.push(1).push(22).push(19)); // OK, 42
立即出现的错误很容易修复:
Node const *current = &n;
// ^^^^^
投诉是current=current->next
正在将一个节点常量*
分配给一个节点*
,所以不要这样做
这样做会使程序编译但打印0
,因为push
都不会调用modifieda
。您也不能将push
的结果存储为constexpr
,因为自动局部变量a
的地址不是常量表达式
但是,您可以形成临时节点的链接列表并立即使用它:
constexpr auto result = getSum(a.push(1).push(22).push(19)); // OK, 42
哇,我不认为你所做的是可能的…
constexpr
对象仍然遵循抽象机器的规则。对push
的所有调用都返回一个立即过期的临时值。即使你修正了错误,你的名单也永远不会增长。该死的。。这一定是我见过的最常的const
和constexpr
哇,我不认为你所做的是可能的…constexpr
对象仍然遵循抽象机器的规则。对push
的所有调用都返回一个立即过期的临时值。即使你修正了错误,你的名单也永远不会增长。该死的。。这一定是我见过的最多的const
和constepr
。这是可能的,我们只需要关注临时对象的生命周期:)这是可能的,我们只需要关注临时对象的生命周期:)