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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:39:46+00:00 2026-05-15T10:39:46+00:00

How do I make a class whose interface matches double, but whose templated types

  • 0

How do I make a class whose interface matches double, but whose templated types do not dynamic cast to double?

The reason is that I have a run-time type system, and I want to be able to have a type that works just like double:

template<int min_value, int max_value>
class BoundedDouble: public double {};

And then use template specialization to get run-time information about that type:

template<typename T>
class Type { etc. }

template<int min_value, int max_value>
class Type<BoundedDouble<min_value, max_value>> { int min() const { return min_value; } etc. }

But, you can’t inherit from double…

  • 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-15T10:39:47+00:00Added an answer on May 15, 2026 at 10:39 am

    You can’t derive from native types. Use composition instead:

    #include <cstdlib>
    #include <string>
    #include <stdexcept>
    #include <iostream>
    using namespace std;
    
    template<typename Type = double, const Type& Min = -10.0, const Type& Max = 10.0> class Bounded
    {
    public:
        Bounded() {};
        Bounded(const Type& rhs) : val_(rhs) 
        { 
            if(rhs > Max || rhs < Min) 
                throw logic_error("Out Of Bounds"); 
        }
    
        operator Type () const 
        { 
            return val_; 
        }
    
        Type val_;
    };
    
    
    int main()
    {
        typedef Bounded<double, -10.0, 10.0> double_10;
        double_10 d(-4.2);
        cout << "d = " << d << "\n";
        double d_prime = d;
        cout << "d_prime = " << d_prime << "\n";
        double_10 d2(-42.0);
        cout << "d2 = " << d << "\n";
    
    
        return 0;
    }
    

    The output is:

    d = -4.2
    d_prime = -4.2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i make one class file for jar. and this class have use wurfl. and
I make a class level method for Alert: @interface TestAlert @end + (void)showErrorAlert:(NSTimer *)message
I am trying to make a class that will represent two different behaivours at
I have a singleton class, whose instance get initialized at global scope within the
I have 3 classes whose names are representative and i intend to make this
I'm trying to make a component manager class in C++ that is similar to
I am a Java newbie, hence the simple question: I have a class whose
I have an interface that contain the method void DoCommand(); . now I'll force
I have a class whose constructor is defined as internal, which means I cannot
I have some logic, which defines and uses some user-defined types, like these: class

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.