-
Notifications
You must be signed in to change notification settings - Fork 261
[BUG] Mutable member functions shouldn't be constant #266
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
Comments
You should mark the
|
Thanks. You're right, and it works as intended now. By the way CPP2 should output a compilation error message. |
Yes, a compilation message should be emitted. I guess it's because of the reason that Herb is not done implementing classes yet. |
Currently, classes still need to be finished. Herb wrote in the commit (258190e) message:
I have been playing with it, and I explored how to use it: N : namespace = {
t : type = {
i : int;
public j : int = 1;
protected k : std::pair<int,int> = (1,2);
f0: () = {
std::cout << "f0" << std::endl;
}
private f1: (this) = {
std::cout << "f1" << std::endl;
}
protected f2: (virtual this) = {
std::cout << "f2" << std::endl;
}
// f3: (override this) = {
// std::cout << "f3" << std::endl;
// }
f4: (implicit this) = {
std::cout << "f4" << std::endl;
}
// f5: (final this) = {
// std::cout << "f5" << std::endl;
// }
f6: (in this) = {
std::cout << "f6" << std::endl;
}
f7: (inout this) = {
std::cout << "f7" << std::endl;
}
// f8: (out this) = {
// std::cout << "f8" << std::endl;
// }
f9: (move this) = {
std::cout << "f9" << std::endl;
}
}
} Generates: namespace N {
class t {
private: int i;
public: int j {1};
protected: std::pair<int,int> k {1, 2};
public: static auto f0() -> void{
std::cout << "f0" << std::endl;
}
private: auto f1() const -> void{
std::cout << "f1" << std::endl;
}
protected: virtual auto f2() const -> void{
std::cout << "f2" << std::endl;
}
// f3: (override this) = {
// std::cout << "f3" << std::endl;
// }
public: auto f4() const -> void{
std::cout << "f4" << std::endl;
}
// f5: (final this) = {
// std::cout << "f5" << std::endl;
// }
public: auto f6() const -> void{
std::cout << "f6" << std::endl;
}
public: auto f7() -> void{
std::cout << "f7" << std::endl;
}
// f8: (out this) = {
// std::cout << "f8" << std::endl;
// }
public: auto f9() && -> void{
std::cout << "f9" << std::endl;
}
};
}; Commented sections are the ones that do not work yet. |
We have cv-qualifiers and ref-qualifiers for member functions in C++, instead we have In C++ the following combinations of qualifiers are available (ignore
But your test examples have different results, does CPP2 preserve overload resolution semantic of C++? |
@msadeqhe in P0708
// qualifying “this” on member functions
auto f() in {}. // present me an X I can read from
auto f() inout {}. // present me an X I can read from and will write to
auto f() out {}. // present me an X I will assign to
auto f() move {}. // present me an X I will move from
auto f() forward {} // present me an X I will pass along
The syntax is not the same but the meaning is similar. I don't know what |
Note that And
It's the opposite of Note: The next commit will have more checking, to allow |
Thank you for the explanation! I wasn't precise about |
Added initial basic support. Includes: - access-specifiers: public, protected, private - defaults: in type scope (aka members), types and functions are public by default, objects (data members) are private by default - this-specifiers: implicit, virtual, override, final (but can't use them much since we don't have inheritance syntax yet, see next list) - explicit `this` parameters (where the this-specifiers go) NOT included yet: - special member functions (`operator=`) - inheritance / base classes - metaclass functions to apply checks, defaults, and generated code (that'll be last...) This checkin also changes the null/subscript/comparison checking opt-out command-line option from `/xxx-checks-` (which was only because the initial default was to opt into the checks) to `/no-xxx-checks`
@msadeqhe Also note that the intentional parameter passing in Cpp2 doesn't have to cover all the options Cpp1 allows today, because today's approach is low-level and allows combinations that don't make sense. You had one such example in your code:
Right, that's similar to an ordinary parameter like
and in both cases the combination doesn't make sense: In Cpp1 this comes up (and we have to teach why it doesn't make sense) because the low-level "how to pass a parameter" mechanical detail-oriented approach allows all the combinations, not only the ones that make sense. This is something that naturally disappears when you have a high-level "what I will use the parameter for" declarative usage approach, which also eliminates most need to overload them. Cpp1 has a few of these things that I aim to eliminate... |
So, are we not getting named contructors? 😕 Edit: Referring to the named contructors as in parameter passing paper. |
I like that construction and assignment will be unified via the = operator, can you just use the factory pattern for "named" constructors?
On 9 March 2023 19:14:03 Abhinav00 ***@***.***> wrote:
And out this on a non-operator= function is disabled because the only way for it to be useful would be to allow calling the function on an uninitialized object. I may allow that extension, but it seems a bit novel and I want to see actual motivating use cases down the road before spending time allowing and teaching it.
So, are we not getting named contructors? 😕
—
Reply to this email directly, view it on GitHub<#266 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQLDOSJNGR6L7SA3V23W3ITXRANCNFSM6AAAAAAVSHRC2U>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Good news: In cppfront today you effectively already have named constructors throughout the language 😁 because any function with an The main difference I mean above is that if I were to allow a non- (*) I say "delegating constructor" a couple of times there just to emphasize that already a Cpp2 function with an |
Thanks for clarifying!! |
Sure thing, quick ack:
That's a non-const function, so it would be
This has come up in a couple of issue, see #198 for example which summarizes the state of this for the time being. There's also some discussion in the d0708 paper. |
Why is
Yes, that's why maybe one issue where we can discuss would be better instead of same thing scattered across various (closed) issues. I'll also mention all the issues it has come up in if I open a new issue. |
Describe the bug
All member functions are constant even if they change the class state (e.g. member variables).
To Reproduce
This is the sample CPP2 code:
I've used Compiler Explorer.
It should generate the following C++ code:
But it generates the following C++ code:
Additional context
function
mutableCall
shouldn't beconst
because it changes the class state (e.g. member variables).The text was updated successfully, but these errors were encountered: