Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the function address literals in c++?

UPDATE: After some additional reading, what I really wanted was guaranteed early binding (which should translated to an immediate call for non-virtual functions and non-PIC code), which can be done by passing a (member) function as a template parameter. The problem I had was that gcc < 4.5 and icc 11.1 can generate some funky instructions for member function pointer template parameter calls. AFAICT, gcc >= 4,5 and vs2008 handle these template parameter calls fine.

First of all, maybe literals is not the right term for this concept, but its the closest I could think of (not literals in the sense of functions as first class citizens).

The idea is that when you make a conventional function call, it compiles to something like this:

callq <immediate address>

But if you make a function call using a function pointer, it compiles to something like this:

mov    <memory location>,%rax
callq  *%rax

Which is all well and good. However, what if I'm writing a template library that requires a callback of some sort with a specified argument list and the user of the library is expected to know what function they want to call at compile time? Then I would like to write my template to accept a function literal as a template parameter. So, similar to

template <int int_literal> 
struct my_template {...};` 

I'd like to write

template <func_literal_t func_literal>
struct my_template {...};

and have calls to func_literal within my_template compile to callq <immediate address>.

Is there a facility in C++ for this, or a work around to achieve the same effect? If not, why not (e.g. some cataclysmic side effects)? How about C++0x or another language?

like image 907
academicRobot Avatar asked May 20 '10 21:05

academicRobot


People also ask

Where literals are stored in C?

String literals are stored in C as an array of chars, terminted by a null byte. A null byte is a char having a value of exactly zero, noted as '\0'. Do not confuse the null byte, '\0', with the character '0', the integer 0, the double 0.0, or the pointer NULL.

Where are literals stored in memory?

The characters of a literal string are stored in order at contiguous memory locations. An escape sequence (such as \\ or \") within a string literal counts as a single character. A null character (represented by the \0 escape sequence) is automatically appended to, and marks the end of, each string literal.

What are the 6 types of literals?

Primitive data types include int, byte, short, float, boolean, double, and char, whereas non-primitive data types include arrays, string, and classes. The primitive literals in java int, byte, short, float, boolean, double, and char represent certain signed integer values.


1 Answers

If you use a function pointer type in your template and instantiate it with a fixed function, then the compiler should use a direct call for that function pointer call.

like image 114
Chris Dodd Avatar answered Oct 19 '22 13:10

Chris Dodd