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

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer yup the other variable needs to initialized or ordered in… May 16, 2026 at 3:39 pm
  • Editorial Team
    Editorial Team added an answer From this post: A "halfway vector" (if you mean that… May 16, 2026 at 3:39 pm
  • Editorial Team
    Editorial Team added an answer First thing first, never declare your properties using var keyword,… May 16, 2026 at 3:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a class whose constructor is defined as internal, which means I cannot
I have a singleton class, whose instance get initialized at global scope within the
I have some logic, which defines and uses some user-defined types, like these: class
I have a colleague in my company whose opinions I have a great deal
I don't know what I'm doing wrong here. I have a ListBox whose DataContext
I'm trying to optimize the size of my Delphi classes so that they take
I'm thinking about designing a method that would return an object that implements an
WS to make a web service. As parameters I am taking in two strings
In college I took on a class on modern physics, in which we learned
I must develop a simple web application to produce reports. I have a single

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.