Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cling] error: function definition is not allowed here when trying to overload operator<= #9449

Closed
sbmorphe opened this issue Dec 16, 2021 · 2 comments · Fixed by #11265
Closed

Comments

@sbmorphe
Copy link

sbmorphe commented Dec 16, 2021

When trying to define an operator in the cling cli repl, the "function definition is not allowed here" error is generated. "cling -version" reports "0.8". Here's a copy/paste of a session to demonstrate the issue:

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <iostream>
[cling]$ using namespace std;
[cling]$ struct A {
[cling]$ ?   A(int val1, int val2) : pod1(val1), pod2(val2) {}
[cling]$ ?   int pod1, pod2;
[cling]$ ?   };
[cling]$ bool operator<=(const A &lhs, const A &rhs) {
[cling]$ ?   return ((lhs.pod1 <= rhs.pod1) && (lhs.pod2 <= rhs.pod2));
[cling]$ ?   }
input_line_6:2:46: error: function definition is not allowed here
 bool operator<=(const A &lhs, const A &rhs) {
                                             ^
@jalopezg-git
Copy link
Contributor

@sbmorphe, thanks for reporting! This is a good catch!

In this case, cling is trying to wrap the input as it thinks bool operator<=(...) is not a valid function declarator -presumably due to the <=. This seems to be another limitation of the current strategy to generate wrapper functions.

See also the related issue #9171. @vgvassilev and I recently talked about this, which has to be addressed also for clang-repl.

As a workaround, you can either temporarily turn on raw input, or define the function in an anonymous namespace, i.e.

[cling]$ .rawInput 1
[cling]$ bool operator<=(const A &lhs, const A &rhs) { return ((lhs.pod1 <= rhs.pod1) && (lhs.pod2 <= rhs.pod2)); }
[cling]$ .rawInput 0

or

[cling]$ namespace { bool operator<=(const A &lhs, const A &rhs) { return ((lhs.pod1 <= rhs.pod1) && (lhs.pod2 <= rhs.pod2)); } }

@jalopezg-git jalopezg-git changed the title function definition is not allowed here [cling] error: function definition is not allowed here when trying to overload operator<= Dec 16, 2021
jiangyilism added a commit to jiangyilism/root that referenced this issue Aug 28, 2022
Fixes root-project#9449

so it is possible to define operator overload in cling cmd prompt.

btw, Make SkipPointerRefs() to not assume next token being a

tok::raw_identifier since it can be a global-scoped identifier,

e.g. int* ::class_a::func_b(short c) { return nullptr; }
@jalopezg-git jalopezg-git linked a pull request Dec 22, 2022 that will close this issue
2 tasks
jiangyilism added a commit to jiangyilism/root that referenced this issue Jun 7, 2023
Fixes root-project#9449

so it is possible to define operator overload in cling cmd prompt.

btw, Make SkipPointerRefs() to not assume next token being a

tok::raw_identifier since it can be a global-scoped identifier,

e.g. int* ::class_a::func_b(short c) { return nullptr; }
jiangyilism added a commit to jiangyilism/root that referenced this issue Jun 7, 2023
Fixes root-project#9449

so it is possible to define operator overload in cling cmd prompt.

btw, Make SkipPointerRefs() to not assume next token being a

tok::raw_identifier since it can be a global-scoped identifier,

e.g. int* ::class_a::func_b(short c) { return nullptr; }
jiangyilism added a commit to jiangyilism/root that referenced this issue Jun 16, 2023
Fixes root-project#9449

so it is possible to define operator overload in cling cmd prompt.

btw, Make SkipPointerRefs() to not assume next token being a

tok::raw_identifier since it can be a global-scoped identifier,

e.g. int* ::class_a::func_b(short c) { return nullptr; }
jalopezg-git pushed a commit that referenced this issue Jul 18, 2023
Fixes #9449

so it is possible to define operator overload in cling cmd prompt.

btw, Make SkipPointerRefs() to not assume next token being a

tok::raw_identifier since it can be a global-scoped identifier,

e.g. int* ::class_a::func_b(short c) { return nullptr; }
FonsRademakers pushed a commit to root-project/cling that referenced this issue Jul 18, 2023
Fixes root-project/root#9449

so it is possible to define operator overload in cling cmd prompt.

btw, Make SkipPointerRefs() to not assume next token being a

tok::raw_identifier since it can be a global-scoped identifier,

e.g. int* ::class_a::func_b(short c) { return nullptr; }
@github-actions
Copy link

Hi @jalopezg-git,

It appears this issue is closed, but wasn't yet added to a project. Please add upcoming versions that will include the fix, or 'not applicable' otherwise.

Sincerely,
🤖

maksgraczyk pushed a commit to maksgraczyk/root that referenced this issue Jan 12, 2024
Fixes root-project#9449

so it is possible to define operator overload in cling cmd prompt.

btw, Make SkipPointerRefs() to not assume next token being a

tok::raw_identifier since it can be a global-scoped identifier,

e.g. int* ::class_a::func_b(short c) { return nullptr; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants