xeus-cling icon indicating copy to clipboard operation
xeus-cling copied to clipboard

[cling] Function definition is not allowed here

Open SylvainCorlay opened this issue 7 years ago • 42 comments

Where declaring a variable before defining a function in cell, we get:

split

However, declaring the variable after the function or separating the two statement in two cells works fine.

SylvainCorlay avatar Aug 08 '17 16:08 SylvainCorlay

If you invert the order it works. So, I am not sure that it is a xeus-cling issue...

split

gouarin avatar Aug 12 '17 06:08 gouarin

A variant: a single cell containing two functions:

int f(int i) {
    return 2;
}
int g(int i) {
    return 3;
}

triggers an error:

input_line_7:5:15: error: function definition is not allowed here
 int g(int i) {
              ^

nthiery avatar Sep 30 '17 09:09 nthiery

Other variant:

using namespace std;
int f(int i) {
    return 1;
}

nthiery avatar Sep 30 '17 10:09 nthiery

This has been solved upstream and will be resolved when we update to the latest version of cling.

SylvainCorlay avatar Oct 03 '17 09:10 SylvainCorlay

This has been solved upstream and will be resolved when we update to the latest version of cling.

Great!

nthiery avatar Oct 03 '17 13:10 nthiery

For the record, another instance:

vector<int> f() {
     return {};
}

triggers also a function definition not allowed here. Alas we can't update easily our installation, and this is annoying for the coming week which is about functions and vectors. Would you by any chance have a workaround? One thing that work is to use a

typedef vector<int> tableau

but this adds another thing to explain to our students.

Thanks in advance,

nthiery avatar Oct 08 '17 21:10 nthiery

@nthiery this issue has been fixed in the master branch of cling https://github.com/root-project/cling/issues/182

We hope that we could have a newest version of cling for xeus-cling soon.

A better workaround is to use auto

auto f()
{
    return std::vector<int>{};
}

So you have to follow the rule of AAA (Almost Always Auto) ;)

gouarin avatar Oct 08 '17 21:10 gouarin

On Sun, Oct 08, 2017 at 02:35:28PM -0700, Loic Gouarin wrote:

[1]@nthiery this issue has been fixed in the master branch of cling [2]root-project/cling#182

We hope that we can have a newest version of cling for xeus-cling soon.

Yup.

A better workaround is to use auto auto f() { return std::vector{}; }

So you have to follow the rule of AAA (Almost Always Auto) ;)

Ah right, of course.

That's indeed the solution I would want to teach them in the longer run. However they only have 3 weeks of programming in their fingers, so I had implicitly crossed out the use of auto in my mind. Now I need to think whether I want to keep crossing it out or not. That's one more concept to introduce and we chose to use C++ rather than Python to be more explicit on static types.

Hmm, pondering, pondering, ...

Thanks Loïc!

nthiery avatar Oct 09 '17 07:10 nthiery

Shoot, auto as return type is C++14, and we are for now just using C++ 11. Ok, maybe we should switch, but not tomorrow. I'll use a typedef for now ...

nthiery avatar Oct 09 '17 21:10 nthiery

Does anyone know why this causes an error: function definition is not allowed here:

std::tuple<std::string, int> testTuple()
{
    std::string s = "string"; 
    int i = 0; 
    return std::make_pair(s, i); 
}

momonala avatar Dec 17 '17 13:12 momonala

@momonala this is due to the presence of <, > characters in the return type. If you use auto, your problem is gone ;).

It is fixed in the cling 0.5 version.

gouarin avatar Dec 17 '17 14:12 gouarin

.rawInput funtion()

suniljaya avatar May 08 '18 07:05 suniljaya

This is still failing with cling 0.5:

vector<int> f() {
    return vector<int>
}
input_line_9:2:18: error: function definition is not allowed here
 vector<int> f() {
                 ^
 (info-111) [~] conda list | grep cling                                                                                                                                                                   clangdev                  5.0.0                   cling_1  [cling]  QuantStack
cling                     0.5                           7  [cling]  QuantStack
cling-patches             1                             0    conda-forge
cryptopp                  5.6.5                   cling_0  [cling]  QuantStack
llvmdev                   5.0.0                   cling_1  [cling]  QuantStack
pyzmq                     17.0.0                  cling_0  [cling]  QuantStack
xeus                      0.14.1                  cling_0  [cling]  QuantStack
xeus-cling                0.4.7                         0    QuantStack
zeromq                    4.2.5                   cling_0  [cling]  QuantStack

nthiery avatar Oct 16 '18 18:10 nthiery

Yet another case: instantiation of a friend function (with a Xeus-cling installed by Conda and updated this week):

class Vector3
{
    public :

        Vector3(double x, double y, double z);
        Vector3() = default;

        friend Vector3 operator+(const Vector3& v1, const Vector3& v2);

    private :
    
        double x_ = 0.;
        double y_ = 0.;
        double z_ = 0.;
}; 

// In another cell:
Vector3 operator+(const Vector3& v1, const Vector3& v2)
{    
    Vector3 ret;
    ret.x_ = v1.x_ + v2.x_;
    ret.y_ = v1.y_ + v2.y_;
    ret.z_ = v1.z_ + v2.z_;
    
    return ret;
}

srpgilles avatar Dec 07 '18 12:12 srpgilles

Hi!

The examples are still failing. Any hope for progress? Why has this ticket been closed?

nthiery avatar Aug 29 '20 10:08 nthiery

I second this thread, it's still an issue.

This fails (Interpreter Error: )

vector<vector<int>> SimpleTest() {
    vector<vector<int>> v {{1, 2, 3}, {4, 5, 6}};
    return v;
}
input_line_44:2:35: error: function definition is not allowed here
 vector<vector<int>> SimpleTest() {

This runs just fine:

auto SimpleTest() {
    vector<vector<int>> v {{1, 2, 3}, {4, 5, 6}};
    return v;
}

This runs only with std::vector, not with vector though:

std::vector<int> OneDimensionalTest() {
    std::vector<int> v {1, 2, 3};
    return v;
}

kkufieta avatar Sep 29 '20 14:09 kkufieta

Any reason for not reopening this ticket?

nthiery avatar Oct 07 '20 19:10 nthiery

Do you have the error with cling? If so , this is a bug to report upstream, otherwise we can reopen this issue.

JohanMabille avatar Oct 07 '20 19:10 JohanMabille

Salut Johan!

I just tried, and did not manage:

cling
****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ auto i=3;
[cling]$ void foo() {}

But I am not sure whether this is relevant: it's indeed not clear to me how to reproduce this in plain cling: here how to group the two lines so that they are evaluated at once, like when they are in the same cell.

nthiery avatar Oct 07 '20 20:10 nthiery

This one does fail though with cling:

[cling]$ #include <vector>
[cling]$ using namespace std;
[cling]$ vector<int> f() { return vector<int>(); }
input_line_8:2:18: error: function definition is not allowed here
 vector<int> f() { return vector<int>(); }

So at least this piece is for upstream.

nthiery avatar Oct 07 '20 20:10 nthiery

@nthiery you can define a multiline function in cling, just hit enter after you open the curly brace. You will see a question mark on the new line (and on each new line until you close the curly brace).

JohanMabille avatar Oct 08 '20 06:10 JohanMabille

@nthiery you can define a multiline function in cling, just hit enter after you open the curly brace. You will see a question mark on the new line (and on each new line until you close the curly brace).

Indeed; I just made it a one-liner for conciseness.

What I don't know how to do (and if it even makes sense) is to put two functions at once. As if they were in the same cell.

Oh, but now that you make me think about it; this ought to be valid code, but it's not:

[cling]$ void f() {} void g() {}
input_line_4:2:11: error: function definition is not allowed here

So in summary: several thing in a single cell is behaving like several thing in a single input line for cling. And the latter has limitations.

nthiery avatar Oct 08 '20 16:10 nthiery

Not to beat a dead horse, but yet another example:

using coord_ext = std::array<float, 6>;
using surf_normal = std::array<float, 3>;
using face_data = std::tuple<coord_ext, coord_ext, coord_ext, coord_ext, surf_normal>;

auto generate_quad_strip(std::vector<coord_ext> rib1, std::vector<coord_ext> rib2)
{
    vector<face_data> quad_strip;
    auto rib1_iter = std::begin(rib1),
         rib2_iter = std::begin(rib2),
          rib1_end = std::end(rib1),
          rib2_end = std::end(rib2);

    /*
        [snip]
     */

    return quad_strip;
}

I don't think this really counts as closed.

jm-boley avatar Oct 17 '20 00:10 jm-boley

Are we reopening this ticket since this issue still seems to persist in latest cling release.

EDIT: removing static did the trick for me.

#pragma cling add_include_path(<path_to_eigen>)
#include <Eigen/Dense>

#include <vector>

static Eigen::VectorXd returnARandomVector(){
    Eigen::VectorXd vect;
    vect << 1,3,4,5;
    return vect;
}

static std::vector<std::uint64_t> foo(){
    std::vector<std::uint64_t> vec_int;
    return vec_int;
}

parik-lum avatar May 07 '21 20:05 parik-lum

Hi!

Is it failing for any function now or am I doing something wrong?

#include <iostream>

using namespace std;

void f() {
    cout << "Hi" << endl;
}

The kernel complains about f(). And every modification I tried failed...

lucasturci avatar May 29 '21 00:05 lucasturci

Hi @lucasturci. Have you tried putting the function definition just by itself in a separate cell? IIRC that is still needed,

nthiery avatar May 29 '21 12:05 nthiery

Hi @lucasturci. Have you tried putting the function definition just by itself in a separate cell? IIRC that is still needed,

Yeah, it worked, thanks!

lucasturci avatar May 29 '21 15:05 lucasturci

So... What does xeus-cling remember across cells and what does it forget?

dslowik44 avatar Jan 17 '22 21:01 dslowik44

#include <iostream>
using namespace std;
int main()
    
{
    
    cout<<"Hello jupyter"<<endl;
    
}

input_line_16:5:1: error: function definition is not allowed here { ^

Lancelof2019 avatar Apr 04 '22 06:04 Lancelof2019

I used include but this ca not be displayed in forum.

Lancelof2019 avatar Apr 04 '22 06:04 Lancelof2019