Task: get and convert function address. The meaning is implied as follows:
We have a function
void func() {}; // ΠΈΠΌΠ΅Π΅ΠΌ Π°Π΄ΡΠ΅Ρ 0x500000, ΠΊ ΠΏΡΠΈΠΌΠ΅ΡΡ constexpr size_t inc_pointer = to_size_t(&func) + 5; // (*) Where to_size_t is a macro / constexpr function that converts the address of the function into a number at the compilation stage. The implementation may be as follows:
#define to_size_t(x) (size_t)(x) // C-style ΠΊΠ°ΡΡ #define to_size_t(x) reinterpret_cast<size_t>(x) // C++-style ΠΊΠ°ΡΡ Why I did not write the cast immediately be clear below.
The problem is that the construction (*) turns into the following code
mov eax, 0x500000 // 0x500000 == Π°Π΄ΡΠ΅Ρ ΡΡΠ½ΠΊΡΠΈΠΈ func add eax, 5 ... I want to ensure that the code is
mov eax, 0x500005 ... Attempt 1
Π‘ast 's did not lead to anything good, the result is higher
Attempt 2
template <class T> union converter { T _func; size_t _pointer; constexpr converter(T func) : _func(func) {}; }; #define to_size_t(x) converter<decltype(x)>{x}._pointer #define identity(x) converter<decltype(x)>{x}._value To check constexpr 'a, I use the following functions:
template <size_t N> void print_it() { std::cout << N << std::endl; } The catch is that constexpr union doesn't want to work the way I need
print_it<identity(1)>(); // ΠΎΠΊ, ΠΊΠΎΠΌΠΏΠΈΠ»ΠΈΡΡΠ΅ΡΡΡ (ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠ΅ ΠΊ ΠΏΠΎΠ»Ρ _value) print_it<to_size_t(1)>(); // error C2975: 'N': invalid template argument for 'print_it', expected compile-time constant expression I will listen to your ideas.
PS And the following code generally crashes the studio (2015)
template <class T> constexpr T identity_constexpr(T value) { return value; } print<identity_constexpr<1>>(); // fatal error C1001: An internal error has occurred in the compiler. UPDATE
The answer has been received, apparently the problem will have to be solved by replacing the constructions in an appropriate way already after compilation in the finished executable file.