Errors with xutility (<chrono> <random> C++ standard libraries)

1.1k views Asked by At

I am having an issue writing a simple random number generator using the standard and in Microsoft Visual Studio 2013. When I attempt to compile, it throws a bunch of errors related to the xutility file. I had a bunch of similar errors thrown when I attempted to use the BOOST libraries to accomplish the same thing, so it MAY be an issue with MSVC.

#include <iostream>
#include <chrono>
#include <random>

using std::cout;
using std::endl;

auto seed = std::chrono::high_resolution_clock::now();

int main()
{
    std::uniform_int_distribution<> randNum(1, 6);

    cout << randNum(seed) << endl;

    return 0;
}

Here are the errors it is throwing:

Error 1 error C2039: 'result_type' : is not a member of 'std::chrono::time_point' e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3256

Error 2 error C2146: syntax error : missing ';' before identifier '_Ty1' e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3256

Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3256

Error 4 error C2065: '_Ty1' : undeclared identifier e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3258

Error 5 error C2070: 'unknown-type': illegal sizeof operand e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3258

Error 6 error C2065: '_Ty1' : undeclared identifier e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3259

Error 7 error C2923: 'std::_If' : '_Ty1' is not a valid template type argument for parameter '_Ty2' e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3259

Error 8 error C2955: 'std::_If' : use of class template requires template argument list e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3259

Here is the section of code from xutility that seems to be causing trouble:

    // TEMPLATE CLASS _Rng_from_urng
template<class _Diff, class _Urng>
    class _Rng_from_urng
    {   // wrap a URNG as an RNG
public:
    typedef typename make_unsigned<_Diff>::type _Ty0;
    typedef typename _Urng::result_type _Ty1;

    typedef typename _If<sizeof (_Ty1) < sizeof (_Ty0),
        _Ty0, _Ty1>::type _Udiff;

The class has a LOT more code after this point, but the errors are happening in these last two lines.

By the way, I spent several hours querying different searches to try and find an answer before posting here with no usable results, though that could easily be a deficiency on my part.

Thanks!

With Sebastion's help, all previous errors were resolved, however a new error has been generated:

Modified code:

    std::default_random_engine engine(seed);

    cout << randNum(engine) << endl;

Error:

Error 1 error C2039: 'generate' : is not a member of 'std::chrono::time_point' e:\program files (x86)\microsoft visual studio 12.0\vc\include\random 1618

1

There are 1 answers

3
Sebastian Redl On BEST ANSWER

cout << randNum(seed) << endl;

That's now how you use a distribution. You need to create a local engine that you initialize with the seed, and then pass the engine to the distribution's call operator.

std::default_random_engine engine(seed);
cout << randNum(engine) << endl;

See the example at cppreference for a more elaborate example:

http://en.cppreference.com/w/cpp/numeric/random