13

The function pointer to the __attribute __ ((const)) function?

 3 years ago
source link: https://www.codesd.com/item/the-function-pointer-to-the-attribute-const-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The function pointer to the __attribute __ ((const)) function?

advertisements

How (in GCC/"GNU C") do you declare a function pointer which points to an __attribute__((const)) function? The idea being that I want the compiler to avoid generating multiple calls to the function called through the function pointer when it can cache the return value from a previous call.


typedef void (*t_const_function)(void) __attribute__((const));

static __attribute__((const)) void A(void) {
}

static void B(void) {
}

int main(int argc, const char* argv[]) {
    t_const_function a = A;

    // warning: initialization makes qualified
    // function pointer from unqualified:
    t_const_function b = B;

    return 0;
}

Or just:

__attribute__((const)) void(*a)(void) = A;

Related Articles

C - What is the advantage for the user of const in the parameters of the functions that are not pointers?

I would like to know if there is any advantage for the user when calling a function like A) void func(int i); or a function like B) void func(const int i); As inside the func the parameter i will be copied anyway to the stack (or to wherever the comp

Why does C ++ allow, but ignore the application of const to types of functions?

I get a real kick out of exploring the unusual corners of C++. Having learned about the real types of functions rather than function pointers from this question, I tried messing around with function typing and came up with this bizarre case: typedef

What is the prototype & ldquo; Const int * foo (int) & rdquo; Means, especially in contrast with & ldquo; Int * foo (int) & rdquo; I understand the second only

This question already has an answer here: What is the difference between const int*, const int * const, and int const *? 12 answers I know that int* foo(int) prototype means that foo is a function that takes an integer argument and returns a pointer

CUDA How to access constant memory in the kernel of the device when constant memory is declared in the host code?

For the record this is homework so help as little or as much with that in mind. We are using constant memory to store a "mask matrix" that will be used to perform a convolution on a larger matrix. When I am in the host code I am copying the mask

What is the difference between const int *, const int * const and int const *?

I always mess up how to use const int*, const int * const, and int const * correctly. Is there a set of rules defining what you can and cannot do? I want to know all the do's and all don'ts in terms of assignments, passing to the functions, etc.Read

Why does std :: distance not work on the mix of const and non const const?

This question already has an answer here: auto it = vector.begin() resulting type is not convertible to const_iterator 3 answers Like the question says, I'm wondering about the reason for that. Because I get an error when I try to get the distance be

What is the definition of const-correctness?

I thought the concept of "const-correctness" was pretty well defined, but when I talked to other people about it, it seemed we had different ideas of what it means. Some people say it's about a program having the "const" annotations in

What is the difference between const virtual and virtual const?

I saw that some function in C++ was declared as virtual const int getNumber(); But what is the difference if the function is declared as the following? const virtual int getNumber(); What is the difference between those two?As was already said, there

the qualitative adjustment (const / volatile) may cause ambiguity

Could anyone help me understand why the below code doesn't compile (VS2010) when the getters are const ? Here's the test code: #include <boost/system/error_code.hpp> class socket { public: // setter - throw exception version void non_blocking(bool m

The difference between const string & amp; and string & amp; without const

I wrote a code to calculate the scalar product of two vectors. I can't seem to fully understand the passing by reference and the const. When i put in paramters of the void entervalues function a void entervalues(string& vect, vector& vectt); WITHO

CLR: What is the lifetime of const string values ​​in memory?

Say we have a class with 10000 const string members. class Schema { //Average string length is 20 public const string ID1 = "some.constant.value"; public const string ID2 = "some.other.constant.value"; //... } Not all fields are refere

Why does NSCalendar have two init methods with similar parameter types that accept one of the calendar identifier constants?

Why does NSCalendar have two init methods with similar parameter types that both accept one of the calendar identifier constants? What is the point of having two init methods that do the exact same thing? Does anyone know the reasoning behind why App

What is the difference between const and readonly?

What is the difference between const and readonly and do you use one over the other?Apart from the apparent difference of having to declare the value at the time of a definition for a const VS readonly values can be computed dynamically but need to b

Why is the build-constructor const argument constant?

Vector(const Vector& other) // Copy constructor { x = other.x; y = other.y; Why is the argument a const?You've gotten answers that mention ensuring that the ctor can't change what's being copied -- and they're right, putting the const there does have

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK