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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:29:20+00:00 2026-05-18T01:29:20+00:00

I have a simple const method that wants to generate a random number int

  • 0

I have a simple const method that wants to generate a random number

int Object::const_method() const {
    std::tr1::uniform_int<int> uni(0,100);
    // do some calculation
   return result;
}

This results in your standard (if templafied) const violation error

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.5.1/../../../../include/c++/4.5.1/tr1/random.tcc:910:4:
error: passing ‘const
std::tr1::mersenne_twister’ as ‘this’ argument
of ‘result_type
std::tr1::mersenne_twister<_UIntType,
__w, __n, __m, __r, __a, __u, __s, __b, __t, __c, __l>::operator()() [with _UIntType = long unsigned int,
int __w = 32, int __n = 624, int __m =
397, int __r = 31, _UIntType __a =
2567483615ul, int __u = 11, int __s =
7, _UIntType __b = 2636928640ul, int
__t = 15, _UIntType __c = 4022730752ul, int __l = 18,
result_type = long unsigned int]’
discards qualifiers

Is this doable without a const_cast on this?

  • 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-18T01:29:20+00:00Added an answer on May 18, 2026 at 1:29 am

    Make your mersenne_twister object mutable within your class. Without seeing all your code, (especially the do_something part), we can’t be certain, but I’m guessing that you have an object within your class of type merseene_twister which you are using a function of which is not a const function itself. This is causing the error in your class, because your const function is calling a function on merseen_twister that may change it, violating your const signature.

    // I'm using this as an example.  Yours may differ
    typedef std::mersenne_twister<unsigned int, 32, 624, 
        397, 31, 0x9908b0df, 11, 7, 0x9d2c5680, 
        15, 0xefc60000, 18> MerTwister;
    
    class Object 
    {
        public:
    
        int Object::const_method() const 
        {
           std::tr1::uniform_int<int> uni(0,100);
    
           // do some calculation using the MerTwister object
           return result;
        }
    
    
        private:
        mutable MerTwister twister;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple C function which I declare as: int strLen(const char* str_)
I have a simple regex conversion method that I use to do some minor
I have simple win service, that executes few tasks periodically. How should I pass
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a simple class Name: class Name { private: char nameStr[30]; public: Name(const
I have a simple spinner which has several number options. I want to just
I have a simple log function that needs to print the current date and
I have a simple Clojure program that queries an Oracle database and prints out
i have simple regular expression: ^123$ Matches are for example 123 1234 etc. How

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.