Author |
Topic |
|
jensa
Junior Member
14 Posts |
Posted - Jun 25 2012 : 11:24:09 AM
|
C++ style guides propagate the use of free functions instead of public member functions (e.g. Scott Meyer, "How Non-Member Functions Improve Encapsulation"). It would be really nice if VA would offer free member functions with a first parameter of type X when I type x. in the following code
class X { public: void member(); }; void f(X const&);
X x; x. should now show X::member and f for auto-completion.
When f is selected, the call should be transformed into f(x).
Best regards, Jens |
|
feline
Whole Tomato Software
United Kingdom
19021 Posts |
Posted - Jun 25 2012 : 1:36:20 PM
|
I don't understand. If you want a listbox suggesting the function call "f" why don't you start typing the function name? There will often be many possible functions that take the class as a parameter, so adding all of these functions to the listbox, when you have not typed any part of their name, is likely to be confusing.
If you want a suggestion for the parameter to the function, this sounds like what VA's Smart Suggestions are designed to do:
http://www.wholetomato.com/products/features/smartSuggestions.asp |
zen is the art of being at one with the two'ness |
|
|
jensa
Junior Member
14 Posts |
Posted - Jul 05 2012 : 07:28:50 AM
|
quote: Originally posted by feline
I don't understand. If you want a listbox suggesting the function call "f" why don't you start typing the function name? There will often be many possible functions that take the class as a parameter, so adding all of these functions to the listbox, when you have not typed any part of their name, is likely to be confusing.
Ok, I will try to elaborate my point. For me (and others like Scott Meyer), free functions are part of the interface of a class. I try to minimize the public interface of my classes to contain only necassary functions and provide anything which can be implemented as a free function using only the public interface as a free function. This leads to very small public interfaces and takes concerns out of the class. However, the IDE does not support me when I want to do something with an object of type X, because it only offers the minimal interface of the class, but not the free functions, so I have to look at the header to see which functions are also part of the interface. As we cannot change the language to have something like universal function call as in D (http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394), my thought was to add free functions with type X as first parameter to the suggestions and turn x. <user has selected free function f(X)> into f(x) automatically. The list of suggestions could also be restricted to the same namespace as the class X, because that's good practice.
|
|
|
feline
Whole Tomato Software
United Kingdom
19021 Posts |
Posted - Jul 05 2012 : 10:54:19 AM
|
There is nothing "linking" these free functions to the type of the variable X. Here you are asking for "the set of functions that I know are connected", but if VA scans its symbol database, it is going to have to look for "all functions, anywhere, that take the type classX as their first parameter". At which point, why just classX as their first parameter, what about any class derived from classX, since that is also a "valid" function in this situation.
Do you use any form of naming convention here, to help you identify the free functions for classX? What about putting these free functions into a helper class?
I am not sure I understand how this helps, since instead of having a class that does a set of tasks, you have a class plus a group of "unconnected" free functions. How do you keep track of these free functions, without going to the header file that contains the class, to work out what functions are available? |
zen is the art of being at one with the two'ness |
|
|
jensa
Junior Member
14 Posts |
Posted - Jul 26 2012 : 04:48:06 AM
|
Sorry for the delayed answer but I have been on vacation.
quote: Originally posted by feline
There is nothing "linking" these free functions to the type of the variable X. Here you are asking for "the set of functions that I know are connected", but if VA scans its symbol database, it is going to have to look for "all functions, anywhere, that take the type classX as their first parameter". At which point, why just classX as their first parameter, what about any class derived from classX, since that is also a "valid" function in this situation.
The link is that a member function class X: public Y { public: void f(); }; can be seen as a function taking an implicit parameter of type X. Python makes this obvious by an explicit self parameter. The same analogy is used in C# when using extension methods.
A function with a base-class parameter (in this case Y) can then be seen as a function from the base-class and should also be available, similar to public members from the base-class.
quote:
Do you use any form of naming convention here, to help you identify the free functions for classX? What about putting these free functions into a helper class?
I put all free functions together with the class into a namespace (Why a helper class? They are free functions and do not share any state). The first point where you suggest a naming convention is the problem where I want Visual Assist to help me to find the functions. The rule would be: When the user enters x. or x-> for a variable x of type X, show all functions that 1. are public member of X 2. are in the same namespace as X and accept an object of type X as or any base-classes of X first parameter.
quote:
I am not sure I understand how this helps, since instead of having a class that does a set of tasks, you have a class plus a group of "unconnected" free functions. How do you keep track of these free functions, without going to the header file that contains the class, to work out what functions are available?
"use of non-friend non-member functions improves a class's encapsulation, and a preference for such functions over member functions makes it easier to design and develop classes with interfaces that are complete and minimal (or close to minimal)." (Please read Scott Meyer, How Non-Member functions Improve Encapsulation, http://www.drdobbs.com/how-non-member-functions-improve-encapsu/184401197). It is also recommended in C++ coding standards by Sutter and Alexandrescu, together with the recommendation to put all free function and the class into a common namespace.
Using free functions also offers extensability for classes withouth changing the class itself, something C# offers with extension methods and the D language has recognized and implemented in a nice and clean way: http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394.
Since the advantages are well-known and discussed and the top disadvantage is the inconvenience when typing, I think VA could provide a benefit no other tool offers for C++ developing right now.
Best regards, Jens |
|
|
feline
Whole Tomato Software
United Kingdom
19021 Posts |
Posted - Jul 26 2012 : 1:54:33 PM
|
I understand the usefulness of this from a programmers point of view, what I am asking is how can VA, or any other parsing tool, understand the relationships here.
Consider this code:
std::string testingStringListbox;
testingStringListbox.| at this point, you are going to get a listbox showing every single function in the std namespace that takes a string as the first parameter. This may be what you want, but a lot of the time you are just going to want the member functions for the class std::string.
Do you see my concern here? What you are asking for makes sense to me, but without some reliable way of associating the functions to the class, you are likely to get a lot of noise, and very slow listboxes, since VA has to scan the entire symbol database for functions that match the criteria, especially since it has to compare not only the type of the variable, but also every derived type as well.
While namespaces are used in C++, a lot of C++ code gets by quite happily without using namespaces. This may not be ideal, but it is what we find, and have to deal with. |
zen is the art of being at one with the two'ness |
|
|
|
Topic |
|
|
|