Marco A.
Marco A.

Reputation: 43662

Static function definition that resembles a pointer to member function

I'm looking into the LLVM source code and I never encountered the following syntax:

class BasicBlock {
 public:
  typedef iplist<Instruction> InstListType;
 private:
  InstListType InstList;

  static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) {
    return &BasicBlock::InstList;
  }
}

what does the above define? At first it seemed a normal static function but I don't understand the BasicBlock::* part. Seems like a static function which returns a member function pointer and that directly executes that member function's code.

Upvotes: 3

Views: 83

Answers (3)

Marco A.
Marco A.

Reputation: 43662

Thanks to iavr for the answer. I'm awarding the answer to him but I'd like to add some details here which will hopefully help someone reading this post.

What I asked and as iavr explained to me, might be understood with the following code:

#include <iostream>
using namespace std;

struct customType {
    int b;
    };
struct Instruction {};

class BasicBlock {
public:

 BasicBlock(int a) { InstList.b = a; }
 customType InstList;

  static customType BasicBlock::*getSublistAccess(Instruction*) {
    return &BasicBlock::InstList;
  }
};

int main() {

    BasicBlock bb(90);

    Instruction justForSignature;
    // Get a pointer to a member of type customType through the static function
    customType BasicBlock::* ptrToMember = BasicBlock::getSublistAccess(&justForSignature);

    cout << (bb.*ptrToMember).b; // Parenthesis are necessary, '.' has higher precedence on *
    // Output: 90

    return 0;
}

Try it out: http://ideone.com/hYgfh8

Upvotes: 0

Luca Davanzo
Luca Davanzo

Reputation: 21520

Is a function pointer.
You can read this article for detail.

Upvotes: 0

iavr
iavr

Reputation: 7637

The return type of static member function getSublistAccess is

iplist<Instruction> BasicBlock::*

that is, a pointer to a non-static data member of class BasicBlock, where the data type is iplist<Instruction>.

What getSublistAccess actually returns is &BasicBlock::InstList, that is exactly a non-static data member of class BasicBlock, where the data type is InstListType. i.e., iplist<Instruction>.

Upvotes: 1

Related Questions