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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:19:13+00:00 2026-05-26T18:19:13+00:00

This question is being asked because of this one . C++11 allows you to

  • 0

This question is being asked because of this one.

C++11 allows you to define literals like this for numeric literals:

template<char...> OutputType operator "" _suffix();

Which means that 503_suffix would become <'5','0','3'>

This is nice, although it isn’t very useful in the form it’s in.

How can I transform this back into a numeric type? This would turn <'5','0','3'> into a constexpr 503. Additionally, it must also work on floating point literals. <'5','.','3> would turn into int 5 or float 5.3

A partial solution was found in the previous question, but it doesn’t work on non-integers:

template <typename t>
constexpr t pow(t base, int exp) {
  return (exp > 0) ? base * pow(base, exp-1) : 1;
};

template <char...> struct literal;
template <> struct literal<> {
  static const unsigned int to_int = 0;
};
template <char c, char ...cv> struct literal<c, cv...> {
  static const unsigned int to_int = (c - '0') * pow(10, sizeof...(cv)) + literal<cv...>::to_int;
};
// use: literal<...>::to_int
// literal<'1','.','5'>::to_int doesn't work
// literal<'1','.','5'>::to_float not implemented
  • 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-26T18:19:14+00:00Added an answer on May 26, 2026 at 6:19 pm

    I think the following should work on floats without exponential part (untested):

    template<bool fp, long long num, long long denom, char ...> struct literal;
    
    template<bool fp, long long num, long long denom> struct literal<fp, num, denom>
    {
       static constexpr double value() { return (1.0*num)/denom; }
    };
    
    template<long long num, long long denom, char digit, char... rest>
      struct literal<false, num, denom, digit, rest...>
    {
      static constexpr double value()
      {
        return literal<false, 10*num + (digit-'0'), denom, rest...>::value();
      }
    };
    
    template<long long num, long long denom, char digit, char... rest>
      struct literal<true, num, denom, digit, rest...>
    {
      static constexpr double value()
      {
        return literal<true, 10*num + (digit-'0'), 10*denom, rest...>::value();
      }
    };
    
    template<long long num, long long denom, char... rest>
      struct literal<false, num, denom, '.', rest...>
    {
      static constexpr double value()
      {
        return literal<true, num, denom, rest...>::value();
      }
    };
    
    template<char... c> double operator "" _dbl()
    {
      return literal<false, 0, 1, c...>::value();
    }
    

    How to extend this to also take an exponential part should be obvious.

    Of course one would also want to do some error checking (make sure that the characters are indeed digits).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I asked this question I accepted the answere because it made sense and
I asked the question this way because I can imagine that there's a potentially
This question changed a lot since it was first asked because I didn't understand
Variations of this question have been asked before, but it seems like the issue
I know that this question is being asked before but I did not get
My question is similar to the one being asked here , but I am
I think this question may end up being a bit subjective so I'm marking
Considering this question of SO, where whole C# in-memory compiler is being called. When
Similar to this question , except the console program being wrapped up in the
Being new to test based development, this question has been bugging me. How much

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.