answersLogoWhite

0


Best Answer

In C++, names are either statically bound at compile time, dynamically bound at runtime or (with compile time computation) not bound at all.

With respect to functions, dynamic binding only occurs when a function is invoked through a function pointer. This also applies to virtual functions because the virtual function table (vtable) is simply a list of function pointers that apply specifically to the runtime class of the object the function is invoked against. If we do not know the runtime type (the actual type) of an object at compile time, then we cannot statically bind a virtual function call at compile time. Instead, the compiler must generate code to perform a vtable lookup and then invoke the appropriate function dynamically. However, if the runtime type is known at compile time, then we can statically bind to a virtual function.

Static binding is faster than dynamic binding because we eliminate the indirection required to invoke a function via a pointer variable. Although we can write code to determine the runtime type of an object and thus statically bind all function calls, the overhead far outweighs the otherwise trivial cost of an indirection.

In the case of recursive functions, dynamic binding can only occur if we invoke the function through a function pointer. Once invoked, all recursive calls to that same function are statically bound:

unsigned object::fact (unsigned x) {

return x<2 ? 1 : x * (fact (x-1));

}

Here, the name fact used within the function is implicitly bound to this->fact and is therefore statically bound to object::fact. The initial invocation may or may not have been dynamically bound, but that has no bearing once the function is invoked.

If a function is declared constant expression (constexpr) and is used in a constant expression, the function call (and its binding) can be eliminated completely. Consider the following:

constexpr unsigned fact (const unsigned num) {

return num < 2 ? 1 : num * fact (num - 1);

}

void f () {

unsigned x = fact (7);

// ...

}

A good compiler will replace all of the above with the following:

void f () { unsigned x = 5040;

// ...

}

This is known as compile-time computation. We get the convenience of a function (even a recursive one) but with none of the runtime cost. And since the function is not required at runtime, there is nothing to bind to. However, constant expression functions are useful in that they can also be used in expressions that are not constant. For example:

void g (unsigned x) {

unsigned y = fact (x);

// ...

}

The above call to fact cannot be computed at compile time because x is not constant, so the compiler must statically bind the function instead. Thus we get the advantage of compile time computation when it is possible to do so and static binding when it is not.

If we wish to prevent static binding completely, then we need to use template metaprogramming instead:

template<unsigned N>

constexpr unsigned fact() {

return N * fact<N-1>();

}

template<>

constexpr unsigned fact<1>() {

return 1;

}

template<>

constexpr unsigned fact<0>() {

return 1;

}

Note that we must use specialisation to handle the end cases (where N is 0 or 1). It is not as elegant as the plain constexpr version, however it does guarantee that we only use compile time computation rather than static binding:

void h (unsigned x) {

unsigned y = fact<7>(); // OK: generates equivalent code to unsigned x = 5040;

unsigned z = fact<x>(); // error: x is not constexpr

// ...

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What does bind time have to do with recursion in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How many types of recursion are there in c language?

Recursion in c language is a method where the function calls itself, within or outside the scope. Using Recursion, complicated problems can be divided into smaller parts so that solving them becomes more manageable. The recursion technique is available in Java, JavaScript, and C++.serves the same purpose. The type of Recursion in C • Direct Recursion • Indirect Recursion. Direct Recursion Recursion can call the function n-number of times. In the case of direct Recursion, the function calls itself inside the same position or in the local scope Direct Recursion problems are the Fibonacci series, a program to print 50 natural numbers. Indirect Recursion In the case of Indirect Recursion, a function X calls function Y, and function Y calls any function Z. Under certain conditions, function Z calls function A. In this case, function A is indirectly related to function Z. Indirect Recursion is also known as mutual Recursion, as more than one function runs a program. It is a two-step recursive function call process for making a recursive function call. Below mentioned are also type of Recursion: Tail Recursion No Tail/Head Recursion Linear Recursion Tree Recursion Tail Recursion A function is said to be tail recursion if it calls itself and also calls the last or the previous statement executed in the process. Head Recursion A function is said to be Head Recursion if it calls itself and also calls the first or the beginning statement executed in the process. Linear Recursion A function is said to be a linear recursive function if it makes a single call to itself each time the procedure executes itself and grows linearly depending on the size of the problem. Tree Recursion Tree Recursion is different from linear Recursion. Rather than making only one call to itself, that function makes more than one recursive call to the process within the recursive function. Following are the steps to solve the recursive problem in C: Step 1: Create a function and assign the work a part should do. Step 2: Select the subproblem and assume that the function already works on the problem. Step 3: Get the answer to the subproblem and use it to resolve the main issue. Step 4: The 90% of the problem defined is solved.


What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What is the C program for heap sort using recursion?

123


Is it possible to learn c plus plus without learning c?

No. C++ is an extension of C. By the time you learn C++, you have learned C.


Why do you study turbo c plus plus?

to waste time


Program to get a system time using c plus plus?

time in hours second minute


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c


What is c plus c plus 2c plus c plus c equal?

c + c + 2c + c + c = 6c


B plus b plus b plus c plus c plus c plus c equals?

b + b + b + c + c + c + c = 3b + 4c


What is c plus c plus c equals 6?

The letter c is a variable. Since c appears in all three c+c+c=6, C has to equal the same number each time. So c equals 2!!!!


Symplify c plus c plus c plus c?

4c


How do you make two loops run at the same time in c plus plus?

With two threads.