Page 1 of 1

Noob question

Posted: October 26th, 2017, 10:57 am
by Raimond
Hello peeps, I'm at the snake tutorial right now, wish i had more time for tuts but for now i have one question.
i see chilli did something i can't fully understand and i wish you could help me.
when initializing the rng constructor
rng(std::random_device()()) he used 2 of "()", my question is why.
rng is from "std::mt19937 rng;"

Re: Noob question

Posted: October 26th, 2017, 4:00 pm
by chili
The first () constructs the random_device object, and the second () calls the function operator (the generator function) on that object.

Re: Noob question

Posted: October 26th, 2017, 6:57 pm
by albinopapa
If I do this:

Code: Select all

	std::mt19937 rng( std::random_device()( ) );
I get the error "Function returning function is not allowed".
However, if I do it in a class initializer list:

Code: Select all

class MyClass
{
public:
	MyClass()
		:
		rng( std::random_device()( ) )
	{}
private:
	std::mt19937 rng;
};
Everything builds fine.
If you create a temporary random number generator in a function, you must use braces to construct the random_device and parentheses to call the operator() ( generate function ).

Code: Select all

	std::mt19937 rng( std::random_device{}( ) );

Re: Noob question

Posted: October 27th, 2017, 5:25 pm
by cameron
Think that's because compiler thinks you're declaring a function.