When read some code I saw this unnamed pointer usage as function parameter. What is meaning of this usage ?
RequestAddListItem(QListWidgetItem*)
used in this line (https://socket.io/blog/socket-io-cpp/)
connect(this,SIGNAL(RequestAddListItem(QListWidgetItem*)),this,SLOT(AddListItem(QListWidgetItem*)));
Declaration:
// in mainwindow.h
Q_SIGNALS:
void RequestAddListItem(QListWidgetItem *item);
This is not a real C++ syntax. SIGNAL() macro will convert it to a string and use it as identifier. It is used to identify the correct signal to bind to, by matching to signals declarations in Q_SIGNALS section. This was a chosen convention to use to omit the argument name in these identifiers. It is a natural decision, given that these names would tend to be meaningless, with signal/slot name being good enough description.
In general, argument names in function declarations are optional, they matter only in function definition, as they are necessary to access the arguments. Technically, they are still optional.
If it's part of a function declaration, it means that it doesn't need to be named yet (e.g. in the header). If it's part of the function's definition, it means the parameter isn't used.
It (the parameter) might need to be declared if this function needs to be an overriding virtual function, a callback that needs to be passed, etc. (i.e. to match some function signature).
Related
Here is the code:
virtual bool myFunction(const Waypoints& /*waypoints*/) {
return false;
}
For my understanding, virtual function is for late / dynamic binding. bool is the return type. const Waypoint& is a constant reference. When it is used to formal parameters, it avoids value copy and forbids being changed by the function.
Now, my question is, shall we need a variable name for the formal parameter of this function somehow? I mean, /*waypoints*/ are simply comments, right? Where are the formal parameters then?
The method has one formal parameter of type const Waypoints&. It is unnamed, because it is not used in the method body. This might make sense, because other implementations of the same method might use it (note that the method is virtual). Whether the name of the parameter /*waypoints*/ is commented out, left there or removed altogether is a matter of taste. Some compilers issue a warning when a formal parameter (that does have a name) is not used in the method body, so this might be the reason it was commented out.
A declaration of a function (member or free) never needs names for parameters (although they can be useful for the reader). A definition of a function only needs names for parameters that are used in the body of the function. You can have different names in different declarations, they are ignored.
Where are the formal parameters then?
There is an parameter of type const Waypoints&. It is unnamed in the definition, which is fine because it is unused.
I saw a function written in the following manner:
retVal PreProcessor::TxSlotCB(void *a_pClass, PRE_PARAMS &/*commonParam*/)
{
some code
}
struct PRE_PARAMS
{
uint32_t param1;
uint32_t param2;
};
what is happening in the second parameter? how can it be empty? and is there any way to access to it?
In C++, a formal parameter can be given but anonymous. This means that the actual argument should be given but is not used in the called function.
So it should be given in calling context, it is transmitted, but the called function cannot and does not use it. And the compiler won't give any warnings.
You cannot access it in the body of the function. If you need to access it, change the declaration of the formal parameter to give it some name.
This means that parameter of type PRE_PARAM is not used by this function currently.
So, what happens is when you design a function you decide on the parameters this function would take to fulfill it's purpose.
But later you find that this parameter is not of much significance to this function. However, removing this parameter from function declaration is a tedious job as you have to check for all calls to this function and make sure they confirm to that change.
So, a better way is to not provide the name for argument in function's definition making that parameter redundant.
I need to write a function which takes a variable number of arguements, its essentially a wrapper around a snprintf like function. I understand how to do this in general as shown in Passing variable number of arguments around.
How ever it appears that, atleast in C, we are required to pass one named parameter to the function before the variable arguments start( Why is void f(...) not allowed in C? ).
However the answers to the same questions say this is easily possible in C++. I was wondering how to achieve this, especially since the method for handling vararg functions seems to be the same as C(va_start, etc). Or are they referring to variadic templates?
So basically is it possible to write a variadic function with no named arguements, as shown below, and if so, how?
void f(...){
...
}
p.s. I would like to do this with c++03 if possible.
While C++ language allows writing functions whose parameter list consist only of ..., the language provides no means for accessing the arguments in functions declared with (...) parameter list.
Basically such functions in C++ exist for their behavior in overload resolution (... parameters match any parameter type). The body of such function will not have access to the arguments. If you see no uses for such functions, just don't use them.
Such functions are often used in non-executed context in some well-known template meta-programming techniques, in which case they don't even have to be defined - a mere declaration is sufficient.
I came across a question regarding writing a code to determine unused functions in C++. We can use different data structures to determine unused functions. But before that, we need to parse the code. I have question related to parsing part, how we can differentiate functions declaration and definition and function-calls?
Like,
//function declaration without argument
fun1 ();
//function definition
fun1 () {
// code goes here
}
main () {
fun1 ();
}
Above declaration and call is looks same where as definition part is little bit different from declaration and call.
Other than above scenario, there are multiple scenario for calling a function and functions scope, like two classes having function with same name one function get called within member function (i.e. no explicit calling object required) OR function call using object so, need to understand object's type first to determine which function is actually called.
How can parsing implemented efficiently? How many parsing will required in above scenario?
This is how you can distinguish them:
//function definition
return_type fun1 (args) {
// code goes here
}
Note that function definition has a "return type" before the function name.
Also, note that a function declaration looks exactly the same as its definition. You don't actually need to distinguish them until you see either ; or {. That is the point in which you make the decision whether its a declaration or definition. In your particular application, you don't really care about it because you don't care what the function actually does.
Unfortunately for you, C++ is complicated. To determine which functions are useless, you actually need at least a basic semantic analysis. This includes at least the type system.
What is worse, is that some function may not necessarily be called directly, but through a virtual function. So your static analysis of the code shows only the parent's function getting called, while in reality it's the child's.
extern "C" int func(int *, Foo);
This is from a sample code from class. But I don't understand how the interface is working, one with no variable name, the other with no type. How is that going to work?
When declaring functions you don't need to specify a parameter name, just a type. Foo in this case is a type.
extern "C" tells the compiler it should use a C-style symbol, which more or less means it won't be using name mangling (which C++ uses to allow multiple functions share a name, but use different parameter sets or namespaces).
one with no variable name, the other with no type. How is that going to work?
In the function declaration (and even in definition), variable names are optional, And in your case, Foo is a type, it's not a variable name!
The following program is completely valid even though function f mentions no parameter names!
int f(int)
{
cout << "f(int) is called";
}
int main()
{
f(100);
}
This is a function declaration. You don't need to have a variable name.
The 2nd does have a type, it's Foo.
This is just a prototype. That is to say, it's what's needed to call the function, but not the code that says what the function actually does.
All the compiler needs to know to generate the calling code is the types of the arguments of the function, the function name, and the return type. It doesn't need to know the names of the arguments.
The second argument is a Foo. That's not the name, that's the type.
By using extern "C" you can link a C++ program to C functions.
In your example above, it will turn off name mangling for func so that you can link to code compiled by a C compiler.
C++ compilers need name mangling to differentiate between different functions with the same name. Name mangling is the rule according to which C++ changes function names into function signatures before invoking the linker.
Your assumption is not correct: both parameters have their type specified, and none of them has the name specified. In this case Foo is a type (a struct?) already defined somewhere.
That the parameters have no names is not a problem because this is the declaration of a function: it only serves to let the compiler know the signature (number and types of parameters, plus return type) of the function. It doesn't matter how the formal parameters are named (or if they are named at all). That information is only useful if you are about to write the function body.