Specifying the type of a function parameter, but not a variable

I saw code example similar to this before

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

compared to regular code like this

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

And I wonder what the goal is not to define your type variables for usability? Most people do this just to hint that these api options are not currently used, but to provide backward compatibility in the future?

Is this possible for RTTI or referencing static variables (although the various samples that I saw did not use it for this purpose, they did not even allow template functions, the variables were simply not used). I tried to find this function, but I'm not even sure what it is called or what to look for.

Basically, what are the reasons / advantages / disadvantages for using this approach?

+6
source share
3 answers

This is usually 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 used in this function.

As you said, usually these parameters are not used for a specific 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; } } 
+9
source

In addition to what @dag is mentioned, the definition of a function without a parameter name finds its application in the specialized field. I know that you mentioned that there are no templates in the example you posted, but for completeness, I would like to publish this use case:

Suppose you define an erase function for std containers. But you want different erase_helper do the actual work based on the type of container. A common and acceptable practice is to use traits .

 // Tags for containers struct vector_tag {}; struct map_tag {}; // Trait class template <typename C> struct container_traits; // Specialization of trait class template <typename T, typename A> struct container_traits<std::vector<T, A>> { typedef vector_tag category; }; template <typename K, typename V, typename C, typename A> struct container_traits<std::map<K, V, C, A>> { typedef map_tag category; }; // Helper function template <typename Container, typename X> void erase_helper(Container& c, const X& x, vector_tag) // <-- no param name { // Erase element from vector } template <typename Container, typename X> void erase_helper(Container& c, const X& x, map_tag) // <-- no param name { // Erase element from map } // Function interface template <typename Container, typename X> void erase(Container& c, const X& x) { erase_helper(c, x, typename container_traits<Container>::category()); } 

Here you can see erase_helper has a third parameter with no name. The parameter type tells the compiler to select the correct function in the template creation phase.

+5
source

Parameters are developed prior to implementation in more cases that affect the architecture more directly

Parameters may not be used because:

  • Overloading a virtual function that has more parameters than is required in a derived class
  • Parameters exist for the reason of the story and do not want to change the api yet
  • Consider the need for the future
+3
source

Source: https://habr.com/ru/post/915165/


All Articles