-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
[PARSE] Constness of the API #2224
Conversation
|
@taketwo there is no real reason to use reference I must say. If I have to change a function to get it constness, I would change this: func (int i); To this: func (const int & i); I don't know the impact of using a reference in these cases. Do think that using reference maybe harmful in some situation? |
Remember, a reference is a pointer in the end. The only difference is that it can not point nowhere and that you do not need to dereference. But it is a pointer! So when you pass a reference, you are actually passing a memory address. For large objects this is a good thing, because you avoid copying. But for small, primitive types it is an overhead! So the only change that I would support is switching from |
I agree, I was thinking about 64 bits pointer compare to sizeof(int) which is usually 4 bytes, so 32 bits. So let say instead of int main(int argc, char ** argv) {
const char *const * argv_cp_0 = argv;
const char * * argv_cp_1 = argv;
} When you try to compile it you get the error:
So for argv you need at least |
I bet out of 100 C++ programmers only 1 could figure out without Google what exactly const char *const * const argv implies. Can you explain what this type is? ;) |
I adgree that it is really not common and everybody in messing around what is const when you see To explain what exactly is: const char *const *const argv I recommand to read this article which goes in great details about types in C/C++. Ok, maybe you're not glad with that, so I create a tiny example which give the difference between In short, if you have I hope clearer now. For this PR, I advocate to use |
It's mildly cryptic but strict and precise, I'm game with it. Please preserve the spaces between the const qualifier and the pointer type i.e. |
Nice, I made a proposition in the last commit. |
I see the spacing is fixed and no native types got converted to const references. If I'm not mistaken this doesn't really break the API: before you couldn't use const types, now you can so it shouldn't break functionality for the old cases, just add a new one. It's ok to merge from my side. @taketwo since you were involved in the discussion I'll wait for your final comments/request/merge. |
My personal opinion is that this looks outright crazy: find_switch (const int argc, const char * const * const argv, const char * const argument_name) There are 6 find_switch (int argc, const char * const * argv, const char * argument_name) This is easier to read, does not affect const-correctness, and enables Fabien's use cases. |
@taketwo : you don't want to keep the |
I'm an advocate of not using Sergio would argue that this Edit: in a way, the fact that your by-value argument is |
Ok, so in this case you forbid the use of const prototypes, for example |
I'm not sure I understand your idea. There is no problem to can |
@taketwo You're right, there is no problem in what is proposed in this PR. I have a doubt about it because previously I had some issue but it is in the case I used references, like in the following: void toto(int & i)
{
std::cout << "just & i: " << i << std::endl;
}
void tata(const int & i)
{
std::cout << "const & i: " << i << std::endl;
toto (i);
}
int main(int argc, char ** argv) {
int i = 42;
tata (i);
return 0;
} In this case, you cannot pass a I'm OK with this PR 😸 |
That's because of the references, exactly. |
Update the prototype of every parse functions to make them constness: where the
const
keyword should be, just put it.When I write code, in the client side, I try to use const wherever it's possible. If in the library side, the const interface is not present, I have to remove the
const
keyword from the client side code (or use a temporary variable, or another trick) to be able to use the PCL functions. With the const API, from the client side you can use indifferently mutable or const variables.Even if this PR changes the API, I don't think that it really breaks it. From the client side, every application should remain the same.
Also, I'm not able to say if this PR breaks ABI or not...