-1

My problem is i need to represent a pointer to class's method like integer number. So it's not problem with functions, for example void (*func)() easy cast to number, but when i trying to cast void (&SomeClass::SomeMethod) to integer with any ways compiles says it's impossible

C-style cast from 'void(ForthInterpreter::*)()' to long is not alowed

I tried (size_t)&ForthInterpreter::CodeFLiteral, static_cast<size_t>(&ForthInterpreter::CodeFLiteral) but i got the same errors. Should to suppose there is a principal differense between pointer to function and method but what is it? And how can i cast it succesfully? I use clang++ with C++11 version.

  • 2
    what do you want to do with the integer ? – 463035818_is_not_an_ai Mar 16 '22 at 17:54
  • 1
    A "pointer-to-member" is only a pointer in an abstract sense; it represents an indirection. It is not a *pointer* except by name. – molbdnilo Mar 16 '22 at 18:00
  • 1
    "*i need to represent a pointer to class's method like integer number*" - why? This sounds like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). There is likely to be better solutions. – Remy Lebeau Mar 16 '22 at 18:03
  • note: `long` is not able to safely roundtrip a pointer only `std::intptr_t` or `std::uintptr_t` are able to. However I'm not sure if that extends to pointer to member too. Regardless `long` will cause issues. – Mgetz Mar 16 '22 at 18:08
  • _"suppose there is a principal differense between pointer to function and method but what is it?"_ A pointer to member function can be `virtual`, in which case it is pointing to an unspecified number of functions until it is bound to a specific object instance. The standard does not specify how this must be implemented and certainly does not promise that the implementation does not exceed the size of a `long`. – Drew Dormann Mar 16 '22 at 18:10
  • Not sure what exactly you were trying to achieve, but you might consider: [std::mem_fn](https://en.cppreference.com/w/cpp/utility/functional/mem_fn) – Ranoiaetep Mar 16 '22 at 18:29

2 Answers2

2

for example void (*func)() easy cast to number

No it's not, it just looks like it on your specific machine. There are systems where a pointer is represented as two internal values, for example read about far pointers.

Not to mention the 64-bit problems you're inviting, long is different types in x64 on gcc and cl for example, two very main-stream compilers.

when i trying to cast void (&SomeClass::SomeMethod) to integer with any ways compiles says it's impossible

Absolutely, because not only a class member pointer has the same problem as above, but it absolutely requires a pointer to the object instance itself (usually passed as a register, and again usually ecx or rcx). There's no way you can represent that in a more portable way than a pointer to the correct type.

i need to represent a pointer to class's method like integer number

No you don't, you just want to. There's a difference there. The solution is to adapt to what is possible instead.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • I need it, because i got some code, which operates with pointers to functions and i have to rewrite some functions to methods. And problem comes in part of code, which have been casting function's pointers, because now all of these functions comes like methods –  Mar 16 '22 at 19:47
1

A pointer-to-member is not just a simple pointer, it is much more complex. Depending on compiler implementation, it could be 2 pointers, one to the object and one to the method. Or it could be an object pointer and an offset into a method table. And so on.

As such, a pointer-to-member simply cannot be stored as-is in an integer, like you are attempting to do. So you need to find another solution to whatever problem you are trying to solve by storing a pointer inside an integer.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • IIRC MSVC used to use pointer to member that could be 16bytes on x86 (not x64) systems. [and found a related source](https://stackoverflow.com/a/13875868) – Mgetz Mar 16 '22 at 18:12