Running xeus-cling(v0.13.0) under jupyter notebooks allows to run C++ code in code cells, sometimes.. Note: this is an issue with Jupyter notebook implementation of cling, Xeus -running cling at the command line does not have these problems.
For examples, It seems to randomly remember certain definitions and forget others:
In cell 1:
#include <iostream>
using std::cout;
using std::endl;
In cell 2:
cout << "D\n";
cout <<"D" << endl;
gives:
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)
Also, whether it will accept a function definition or not seems unpredictable to me. Here is an example:
In cell 1:
#include <iostream>
cell 2:
using namespace std;
void f2(){
cout << "HI\n";
}
gives:
input_line_8:3:10: error: function definition is not allowed here
void f2(){
^
Interpreter Error:
while, In cell 3:
using namespace std;
cout << "using std" << endl;
gives:
using std
then, in cells 4 & 5:
void f2(){
cout << "HI, still using std.\n";
}
f2();
happily gives:
HI, still using std.
Is there some explanation out there for what xeus-cling is doing between cells?? how it's interpreting C++ (at a high, user level)? I do not see anything discussing this here readthedocs, or here.
More clues: In cell 1:
void a() {}
void b() {}
gives
error: function definition is not allowed here
void b() {}
and seems can define at most one function per cell is an implicit rule of xeus-cling. Can we make these rules explicit?
Another bug:
struct A {
A(int);
int i;
};
A::A(int x) : i{x} {}
gives:
error: expected '{' or ','
A::A(int x) : i{x} void __cling_Un1Qu31(void* vpClingValue) {
but
A::A(int x) { i = x; }
is accepted. It's actually cling that can't accept class member initializer lists and xeus-cling inherits the bug.
But wrapping the above struct def followed by ctor def in a namespace results in no bug:
namespace aa {
struct A {
A(int);
int i;
};
}
namespace aa {
A::A(int x) : i{x} {}
}
aa::A a{3};
a.i
prints out 3
in cling, and gives:
input_line_8:5:2: error: unknown type name 'a'
a.i;
in xeus-cling, unless a.i
is moved to a new cell where it will give 3
.