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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:44:28+00:00 2026-05-25T21:44:28+00:00

Can I use assert to enforce type definitions. Suppose there is a variable, double

  • 0

Can I use assert to enforce type definitions. Suppose there is a variable, double d, how can you use assert to assert that d is a double? If assert is not applicable (which I am betting isn’t), is there another option? I am specifically looking to test for implicit type casting during debugging, while benefiting from the functionality of assert and #define NDEBUG.

P.S
Obviously I would want to use this for any type definition, just using double as an example here. The solution should be cross platform compatible and be compatible with C++03.

I like to add error checking to my class setters. For example, suppose there is a class, MyClass, with a private member variable, x:

void MyClass::setX(double input)
{
   // assert x is double
   x = input;
}
  • 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-25T21:44:29+00:00Added an answer on May 25, 2026 at 9:44 pm

    It’s really a compile time check, so you should use static asserts for this.

    Here is an example using boost’s static asserts and type traits.

    #include <boost/static_assert.hpp>
    #include <boost/type_traits.hpp>
    
    template<typename T>
      void some_func() {
        BOOST_STATIC_ASSERT( (boost::is_same<double, T>::value) );
      }
    
    TEST(type_check) {
      some_func<double>();
    }
    

    I assume you mean in terms of a template anyway.

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

Sidebar

Related Questions

Is there a straighforward library that I can use to access the CIL for
I have a couple of libraries which use Debug.Assert(...) . I think that the
I need an Http request that I can use in .Net which takes under
Is there some method to use for Assert and Verify. I can't find any
If I want to make a rule dynamic so i can use assert after
I was wondering, I am aware you can use assert to add facts or
I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
Possible Duplicate: Should I use uint in C# for values that can’t be negative?
Can I make assert throw an exception that I choose instead of AssertionError ?
Why can't we use assertion for public methods? I have read somewhere An assert

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.