Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8109083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:18:43+00:00 2026-06-06T01:18:43+00:00

The below program calculates 2 raised to the power n without using any loop,runtime

  • 0

The below program calculates 2 raised to the power n without using any loop,runtime recursion or library function[pow].
It uses TEMPLATE METAPROGRAMMING technique.

#include <iostream>
using namespace std;

template<int n> struct funStruct
{
    enum { val = 2*funStruct<n-1>::val };
};

template<> struct funStruct<0>
{
    enum { val = 1 };
};

int main()
{
    cout << funStruct<8>::val << endl;
    return 0;
}

I was wandering that can i use function in place of structure?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:18:44+00:00Added an answer on June 6, 2026 at 1:18 am

    The obvious solution, as @tdammes points out is just a non-iterative non-recursive approach:

    constexpr int pow2( unsigned pwr ) {
       return 1 << per;
    }
    

    By using constexpr the compiler will compute the result at compile time and yield a compile time constant. If you still want to use recursion, you can:

    constexpr int pow2( unsigned pwr ) {
       return pwr==0? 1 : 2*pow2(pwr-1);
    }
    

    Which is basically the same compile time recursion than your metaprogramming trick in a slightly more concise and easy to read manner. The use of constexpr, of course, requires C++11, so if you don’t have it, you can always use either the original metaprogramming trick, or @tdammers approach adapted:

    template <unsigned int N>
    struct pow2 {
       static const unsigned int value = 1 << N;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My professor wants us to write a program without using arrays or vectors like
When I run the below program I am getting the following errors: (any ideas?)
I'm using Task Parallel Library (TPL ) for calculating Fibonacci number. Program is given
The two programs below get n integers from file and calculates the sum of
Below program contains two show() functions in parent and child classes, but first show()
In the below program,i am not getting values from printf . #include<stdio.h> int main()
I have the below program that will move files from one directory to other.
In this below program i am printing the contents of the div when i
I have a Enumeration as shown in below program public class Test { public
I have a little confusion in the following two statements. The below program is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.