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

  • SEARCH
  • Home
  • 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 7032831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:59:08+00:00 2026-05-28T00:59:08+00:00

In a recent interview, I was asked a really strange question. The interviewer asked

  • 0

In a recent interview, I was asked a really strange question. The interviewer asked me how can I compute 1+2+3+…+1000 just using compiler features. This means that I am not allowed to write a program and execute it, but I should just write a program that could drive the compiler to compute this sum while compilation and print the result when compilation completes. As a hint, he told me that I may use generics and pre-processor features of the compiler. It is possible to use C++, C# or Java compiler. Any ideas???

This question is not related to computing the sum without any loops asked here. In addition, It should be noted that the sum SHOULD be calculated during compilation. Printing just the result using C++ compiler directives is not acceptable.


Reading more about the posted answers, I found that solving problems during compilation using C++ templates is called metaprogramming. This is a technique that was discovered accidentally by Dr. Erwin Unruh, during the process of standardizing the C++ language. You may read more about this topic on wiki page of meta-programming.
It seems that it is possible to write the program in Java using java annotations. You may take a look at maress’s answer below.

A nice book about meta-programming in C++ is this one. Worth to take a look if interested.

A useful C++ meta-programming library is Boost’s MPL this link.

  • 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-05-28T00:59:08+00:00Added an answer on May 28, 2026 at 12:59 am

    Updated Now with improved recursion depth! Works on MSVC10 and GCC without increased depth. 🙂


    Simple compile-time recursion + addition:

    template<unsigned Cur, unsigned Goal>
    struct adder{
      static unsigned const sub_goal = (Cur + Goal) / 2;
      static unsigned const tmp = adder<Cur, sub_goal>::value;
      static unsigned const value = tmp + adder<sub_goal+1, Goal>::value;
    };
    
    template<unsigned Goal>
    struct adder<Goal, Goal>{
      static unsigned const value = Goal;
    };
    

    Testcode:

    template<unsigned Start>
    struct sum_from{
      template<unsigned Goal>
      struct to{
        template<unsigned N>
        struct equals;
    
        typedef equals<adder<Start, Goal>::value> result;
      };
    };
    
    int main(){
      sum_from<1>::to<1000>::result();
    }
    

    Output for GCC:

    error: declaration of ‘struct sum_from<1u>::to<1000u>::equals<500500u>’

    Live example on Ideone.

    Output for MSVC10:

    error C2514: 'sum_from<Start>::to<Goal>::equals<Result>' : class has no constructors
          with
          [
              Start=1,
              Goal=1000,
              Result=500500
          ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This was a question I was asked at my recent interview and I want
Hii , I was asked this question in a recent interview for which i
This recent question about sorting randomly using C# got me thinking about the way
In a recent interview one peculiar question has been asked a[]= { 1,2,3,4,5,6,7,8,9,10} When
I thought I had reasonable answers for this question at a recent interview, but
During a recent interview one of my friends was asked a question to reverse
I was asked the following question at a recent job interview: What do you
I was faced this question while one of recent interview : class1 { virtual
In a recent interview, I was asked to implement a thread safe generic (i.e.template
A recent question came up about using String.Format(). Part of my answer included a

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.