2
struct MyClass {
  int foo () { return 0; }
};

unsigned int size = sizeof(MyClass::foo);  // obviously error

Can we apply sizeof() to member methods from outside the class ? Do we need to declare object to get it ?

Edit: I know that above code will give error (that's why word 'obviously'). Wanted to know if we can at all apply the sizeof() to a member method. I don't want to describe the use case for that in length.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 6
    Why do you need the size of a function? – Jesus Ramos Jul 25 '11 at 06:53
  • Only valid use I can think of is a JMP detour that also clears the entire function block...? – Jesus Ramos Jul 25 '11 at 06:55
  • 2
    Note that you can't apply `sizeof` to a static member as it has function type anyway. – Luc Danton Jul 25 '11 at 06:56
  • @Jesus, In template meta-programming, `sizeof()` trick is used often. I am aware of applying it on normal functions. Sometimes if I come across situation where I need to use it on member function, then how to do it? – iammilind Jul 25 '11 at 06:58
  • 5
    @iammilind: That's nonsense code. You might want to explain what you are trying to do, rather that asking how to pursue your current attempt at doing it. _That said, I don't see how the downvotes are justified. (The same goes for the close-vote.)_ – sbi Jul 25 '11 at 06:59
  • @sbi, I have already mentioned in the comment that it will give an error – iammilind Jul 25 '11 at 07:00
  • 4
    5.3.3 [expr.sizeof] "The `sizeof` operator shall not be applied to an expression that has **function** or incomplete **type**, or to an enumeration type before all its enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates a bit-field." ... "The sizeof operator can be applied to a pointer to a function, but shall not be applied directly to a function." ... "The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and **function-to-pointer** (4.3) standard conversions are **not** applied to the operand of sizeof." – CB Bailey Jul 25 '11 at 07:01
  • 4
    @iammilind: That tends to happen with arbitrary nonsense code. Which is exactly why I was asking what you, actually, tried to achieve. – sbi Jul 25 '11 at 07:02
  • @Charles, why is it a comment? In your comment, I get my answer. – iammilind Jul 25 '11 at 07:05
  • 1
    @iammilind: Because it doesn't answer your question, it only says that I haven't understood your question or that your question can't be answered. – CB Bailey Jul 25 '11 at 07:06
  • @iammilind:Well I can't think of a Use case where one would ever need to do this, but +1 just to nullify the downvote, which is not justified. – Alok Save Jul 25 '11 at 07:13
  • @Als: "can't think of a Use case"... it could give some insight into the cost of faulting in the code for a function function, or having that occupying cache, and whether to ask the compiler to consider inlining (or force it via a compiler extension), or in space/size tuning template instantiations versus other polymorphic and generic mechanisms. If could also be relevant if you have say a SIGSEGV address and want to know if it falls inside a function's code. – Tony Delroy Jul 25 '11 at 07:54

4 Answers4

7

You cannot obtain the size of a member-function, but you can obtain the sizeof a pointer-to-member-function:

int size = sizeof( &MyClass::foo );

The same goes for non-member functions (and static member functions), the size of the function cannot be obtained. It might be misleading because in most contexts, the name of the function decays automatically into a pointer to the function basically in the same way that an array decays to a pointer to the first element, but as in the case of arrays, sizeof does not trigger the decay and that in turn means that you have to ask for the pointer explicitly.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • I think it's more helpful to refer to `&MyClass::foo` as a pointer-to-member. Pointers to member functions are a special case of pointer to members and are very different to "normal" function pointers. – CB Bailey Jul 25 '11 at 07:18
  • Actually, I meant to applying `sizeof()` operator to the function. In my edit I misspelled as sizeof function. Which I have edited now. – iammilind Jul 25 '11 at 07:18
  • 2
    @Charles: I do agree that in general *pointer-to-member*, but in this particular case the special case is not because it is a *member*, but because it is a *function*, the same issue would arise with a non-member function `sizeof( f )` would fail to compile in the same way. I don't think the standard makes a distinction when dealing with *functions* versus *member-functions* with respect to the `sizeof` operator. I have edited anyway hoping that it might be clear, if it is not feel free to edit or telling me. And thanks! – David Rodríguez - dribeas Jul 25 '11 at 07:38
1

iirc this would return size of function pointer anyways, so why do that? Or am I mistaken?

Edit: I was mistaken, this is invalid code, event if function were out of class. All you can do with sizeof and function is get size of function pointer(which you need to make first). If you want to get size occupied by function code you'll need some other way to get that.

Some further reading: http://msdn.microsoft.com/en-us/library/4s7x1k91(v=vs.71).aspx

morphles
  • 2,424
  • 1
  • 24
  • 26
1

Use

sizeof(int (MyClass::*)())

since you're taking the "size of a member function pointer of MyClass that returns int and takes no arguments".

user541686
  • 205,094
  • 128
  • 528
  • 886
  • So do we need to mention the `return` type explicitly; that's same as `sizeof(int)`. Is there a way by which we can write the name of the function and find it ? – iammilind Jul 25 '11 at 07:04
  • 1
    @iammilind: No (unless you have C++0x), because the *entire point* of member function pointers is to avoid naming them. :) In C++0x you can *infer* the type, though, but I don't know exactly how to do it. – user541686 Jul 25 '11 at 07:05
  • 4
    @iammilind: You can just do `sizeof(&MyClass::foo)` if you're happy knowing the size of the pointer-to-member object. – CB Bailey Jul 25 '11 at 07:05
1

Wanted to know if we can at all find the sizeof() a member method.

No, because the C++ language doesn't have such a concept. Or the size of any kind of function.

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
  • If you see my original post, I mentioned `applying sizeof() to function` – iammilind Jul 25 '11 at 07:17
  • @iammilind I have no idea what you mean. – Luc Danton Jul 25 '11 at 07:20
  • See how `is_base_of::value` is defined here: http://stackoverflow.com/questions/2910979/how-is-base-of-works – iammilind Jul 25 '11 at 07:22
  • 1
    @iammilind `sizeof` is never applied to any function there. It's applied to a function *call*. – Luc Danton Jul 25 '11 at 07:24
  • @iammilind: many traits templates use the size of the return type of a function call to determine their truth or falseness... combining this with SFINAE, or the allowed implicit conversion of `false` but not `true` to a pointer, is particularly useful in that varying which function is matched, and hence the return type, varies the size of the return type.... – Tony Delroy Jul 25 '11 at 08:01