Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying function parameter type, but not variable

I have seen example code like this before

class C
{
    C();
    ~C();
    foo(T1, T2);
}

C::foo(T1, T2)
{
    //not using T1/T2
}

versus conventional code like this

class D
{
    D();
    ~D();
    bar(T1 t1, T2 t2);
}

D::bar(T1 t1, T2 t2)
{
    //using t1 and t2
}

And I am wondering what is the purpose of not defining your type variables for usability? Do most people do this just to hint that those parameters of the api are not currently in use, but to ensure backward compatibility in the future?

Is it possibly for RTTI, or referencing static variables (although the various samples I've seen were not using it for this purpose, they werent even templated functions, the variables were simply unused). I've tried searching for this feature but I am not even sure what it is called or what to search.

Basically, what are the reasons/benefits/downsides for using this approach?

like image 828
Faris M Avatar asked May 08 '12 06:05

Faris M


People also ask

Can a function parameter be a variable?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition.

What are the 2 types of function parameters?

Mandatory and Optional Parameters That is, when we initialise a parameter with a default value, it becomes optional. Otherwise, the parameter will be mandatory. In the above example, man1 and man2 are mandatory because they are not initialised in the function definition.

Do function parameters request data type?

Function Parameters are the names that are define in the function definition and real values passed to the function in function definition are known as arguments. Parameter Rules: There is no need to specify the data type for parameters in JavaScript function definitions.

How do you specify parameter data type in Python?

Python is a strongly-typed dynamic language in which we don't have to specify the data type of the function return value and function argument. It relates type with values instead of names. The only way to specify data of specific types is by providing explicit datatypes while calling the functions.


1 Answers

This is commonly done to suppress compiler warnings about unused variables. When you do not create a variable name, the compiler will not warn you that the variable is not being used in the function.

Like you stated, its usually that parameters are not being used for the particular implementation:

class an_iface
{
public:
    an_iface();

    virtual int doSomething(int valNeeded)=0;
}

#define NOT_SUPPORTED -1
class something : public an_iface
{
public:
    something();
    int doSomething (int) { return NOT_SUPPORTED; }
}

class somethingMore : public an_iface
{
public:
    somethingMore();
    int doSomething(int stuff) { return stuff * 10; }
}
like image 171
dag Avatar answered Sep 22 '22 11:09

dag