How should I read this C ++ syntax?

I am probably a complete idiot, but I just saw this C ++ syntax and I can't let my life determine what it does:

(*x)(&a, b, c); 

A quick answer would be very helpful.

+6
source share
9 answers

Well, there can be many possibilities: it all depends on the type of all objects: x , a , b , c . In C ++, you can even overload the comma operator.

But I will focus only on x and see how everything turns out. However, the actual answer will be too long if all combinations are taken into account.

 (*x)(&a, b, c); 

Here x can be one of the following:

  • Function pointer
  • Pointer to a function object.
  • An iterator that, when dereferenced, returns a pointer function or function object.
  • (one more from below, partially covered by the previous one!)

And then you call it, passing it three arguments.

Here are a few examples, counting all the other objects ( a , b c ) as int :

  • Assumption

      int a,b,c; //FIXED as per the assumption 
  • Function Index

      void f(int *,int, int); auto *x = f; (*x)(&a,b,c); //x is function pointer x(&a,b,c); //works fine, even without (*x) 
  • Function object

     struct X { void operator()(int*,int,int); }; X y, *x = &y; (*x)(&a,b,c); //x is pointer to function object 
  • Iterator

     std::list<std::function<void(int*,int,int)> l {X(), f}; auto x = l.begin(); //x is an iterator (*x)(&a,b,c); //(*x) is function object ++x; (*x)(&a,b,c); //(*x) is function object (still!) //OR std::list<void(int*,int,int)> l {f}; auto x = l.begin(); //x is an iterator (*x)(&a,b,c);  //(*x) is function pointer! 

As @David said in a comment that:

However, there is a fourth possibility: x may be an instance of some class that overloads the * operator to return a pointer to a function or function object.

which is true, but I believe that this possibility is partially covered by the iterator, or at least the example of the iterator gave you enough hint to figure it out yourself. :-)

Hope this helps.

+20
source

It seems that x is a pointer to a function. If so, then this means:

  • dereference x to get a function type expression;

  • calling this function with arguments addressof a , b and c .

Also note that the * (dereferencing) and () operators around x are redundant; You can write

 x(&a, b, c); 

if x is a function pointer.

+7
source

Without knowing the definitions of characters, it is impossible to be 100% sure, but I suspect that this is a function call. (Assuming x is a function pointer, if x is of class type, it will depend on the unary * overload in the class.) Thus, (*x) is a pointer dereferencing and is logically a function; Given this, the rest should be obvious. (Note that formally a function decomposes into a pointer to a function in most contexts, and you call a pointer to a function. Therefore, if x is a pointer to a function (*x) and x , then they are identical, because unlike the compiler. However, not so human readers, and most will appreciate that the challenge is indirect.)

+3
source

The following is an example of using operator() , which can create an identical signature.

 #include <iostream> using namespace std; struct sample { void operator()(int* a, int b, int c) { *a = b = c; return; } }; int main() { sample* x; int a = 1, b = 1, c = 1; (*x)(&a, b, c); return 0; } 
+2
source

x is probably a function pointer or a pointer to a function pointer.

+1
source

It calls a function through a function pointer.

The call function is indicated by the pointer 'x' with the arguments provided.

This link may be useful: C ++ Function Tutorial

+1
source

This is most likely a function call that the function pointer points to. A more conditional syntax for it is as follows:

 x(&a, b, c); 

x indicates a function that takes three arguments. The call expression makes a difference between the pointer and passes the three arguments to the function pointed to by the pointer.

An alternative could be that x is a pointer to an object that defines an override for an operator with three arguments () . To find out which one you need, you need to provide more context.

+1
source

It looks like x is either a pointer to a pointer to a function, or a pointer to a functor. &a, b, c is a list of arguments to invoke.

0
source

It passes the arguments &a, b, c function pointed to by *x

0
source

All Articles