How is Xeus-cling interpreting C++? Practical, User explanation -forgotten defs and function defs not allowed

1k Views Asked by At

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.

1

There are 1 best solutions below

2
Abdul ahad On

It looks like this is how the xeus-cling works like the explanation is given in this link:

Jupyter notebook error for C++ Kernel[cling]

Some rules are given in this link: https://code-ballads.net/generated-notebooks/cpp/repl_cling/notebooks/3_Advices_And_Gotchas.html

if cling repeatedly fails to compile/evaluate a cell (Function definition not allowed here, or various type errors), the kernel may have gotten corrupted / in a bad state, so just use Kernel -> Restart And Run All Cells. Actually the xeus-cling works as a interpreter that's why it reads only one instruction in a single cell and doesn't act like compiler.

Details are provided in this link: https://blog.jupyter.org/interactive-workflows-for-c-with-jupyter-fe9b54227d92

enter image description here

The difference between compiler and interpreter is given in this link: https://www.businessinsider.in/difference-between-compiler-and-interpreter/articleshow/69523408.cms#:~:text=Compliers%20and%20interpreters%20are%20programs,be%20understood%20by%20the%20computers.&text=Compiler%20scans%20the%20entire%20program,to%20analyze%20the%20source%20code. enter image description here