在 jupyter notebooks 下运行 xeus-cling(v0.13.0) 允许在代码单元中运行 C++ 代码,有时.. 注意:这是 Jupyter notebook 实现 cling 的问题,Xeus -running cling 在命令行没有这些问题.
例如,它似乎会随机记住某些定义而忘记其他定义:
在单元格 1 中:
#include <iostream>
using std::cout;
using std::endl;
在单元格 2 中:
cout << "D\n";
cout <<"D" << endl;
给出:
input_line_9:3:15: error: use of undeclared identifier 'endl'; did you mean 'std::endl'?
cout <<"D" << endl;
^~~~
std::endl
/home/don/miniconda3/envs/xeus-cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:599:5: note: 'std::endl' declared here
endl(basic_ostream<_CharT, _Traits>& __os)
此外,它是否会接受函数定义对我来说似乎是不可预测的。这是一个例子:
在单元格 1 中:
#include <iostream>
单元格 2:
using namespace std;
void f2(){
cout << "HI\n";
}
给出:
input_line_8:3:10: error: function definition is not allowed here
void f2(){
^
Interpreter Error:
同时,在单元格 3 中:
using namespace std;
cout << "using std" << endl;
给出:
using std
然后,在单元格 4 和 5 中:
void f2(){
cout << "HI, still using std.\n";
}
f2();
高兴地给出:
HI, still using std.
是否有一些解释 xeus-cling 在细胞之间的作用?它如何解释 C++(在高级用户级别)?我在此处阅读文档或此处没有看到任何讨论此内容的内容。
更多线索:在单元格 1 中:
void a() {}
void b() {}
给
error: function definition is not allowed here
void b() {}
并且似乎每个单元最多可以定义一个函数是 xeus-cling 的隐含规则。我们可以明确这些规则吗?
另一个错误:
struct A {
A(int);
int i;
};
A::A(int x) : i{x} {}
给出:
error: expected '{' or ','
A::A(int x) : i{x} void __cling_Un1Qu31(void* vpClingValue) {
但
A::A(int x) { i = x; }
被接受。它实际上是 cling 不能接受类成员初始化器列表,而 xeus-cling 继承了这个 bug。
但是将上述 struct def 后跟 ctor def 包装在命名空间中不会导致错误:
namespace aa {
struct A {
A(int);
int i;
};
}
namespace aa {
A::A(int x) : i{x} {}
}
aa::A a{3};
a.i
3
在紧贴中打印出来,并给出:
input_line_8:5:2: error: unknown type name 'a'
a.i;
在 xeus-cling 中,除非a.i
被移动到一个新的单元格,它将给出3
.