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 6562987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:46:31+00:00 2026-05-25T13:46:31+00:00

I am preparing myself for the defintion of user-defined literals with a Variadic Template

  • 0

I am preparing myself for the defintion of user-defined literals with a Variadic Template

template<...>
unsigned operator "" _binary();

unsigned thirteen = 1101_binary;

GCC 4.7.0 does not support operator "" yet, but I can simulate this with a simple function until then.

Alas, my recursion is the wrong way around. I can not think of a nice way how I do not shift the rightmost values, but the leftmost:

template<char C> int _bin();
template<>       int _bin<'1'>() { return 1; }
template<>       int _bin<'0'>() { return 0; }

template<char C, char D, char... ES>
int _bin() {
    return _bin<C>() | _bin<D,ES...>() << 1; // <-- WRONG!
}

which of course is not quite right:

int val13 = _bin<'1','1','0','1'>();  // <-- gives 10

because my recursion shifts the rightmost ‘1’s farthest, and not the leftmost ones.

It is probably I tiny thing, but I just can not see it.

  • Can I correct the line _bin<C>() | _bin<D,ES...>() << 1;?
  • Or do I have to forward everything and turn it around everything afterwards (not nice)?
  • Or any other way that I can not see?

Update: I could not fold the recursion the other way around, but I discovered sizeof.... Works, but not perfect. Is there another way?

template<char C, char D, char... ES>
int _bin() {
    return   _bin<C>() << (sizeof...(ES)+1) | _bin<D,ES...>() ;
}
  • 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-25T13:46:31+00:00Added an answer on May 25, 2026 at 1:46 pm

    At any one step of the recursion you already know the rank of the leftmost digit.

    template<char C> int _bin();
    template<>       int _bin<'1'>() { return 1; }
    template<>       int _bin<'0'>() { return 0; }
    
    template<char C, char D, char... ES>
    int _bin() {
        return _bin<C>() << (1 + sizeof...(ES)) | _bin<D,ES...>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently preparing myself for the exam in Networking and I got some
I'm preparing my paypal system and have a separate page that forwards the user
Just preparing myself for a task that i feel could be quite troublesome, and
I'm trying to code to refresh my memory preparing myself for a course. int
i am preparing myself for importing data from excel sheet to sql server 2005.I
Pros and cons and Usage I have been preparing myself for an interview for
I am preparing myself for a lecture exam on security aspects of software development.
I am preparing myself for programming competitions and i would like to know how
I am preparing myself for an interview that I have on monday and I
I'm not great with MySQL, so I often find myself preparing sub-optimal queries that

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.