I have Random class, and I don`t know how correctly initialize its static data members.
// random.h
#pragma once
#include <random>
class Random
{
private:
static std::uniform_real_distribution<float> sDistribution_;
static std::mt19937 sGenerator_;
};
// random.cpp
#include "random.h"
std::mt19937 Random::sGenerator_(std::random_device()); // error here
std::uniform_real_distribution<float> Random::sDistribution_(0.0f, 1.0f);
When I compile this, I get an error:
declaration is not compatible with
std::mt19937.
How do I correctly initialize this member?
is an instance of the most vexing parse. You wanted to perform direct initialization, but you're actually:
This is what the compiler thinks, and it conflicts with the original definition of
Random::sGenerator_, which declares it as a static data member of typestd::mt19937.The solution is very simple:
std::random_deviceis a type, not a function, so you need to initialize it before calling it, like:In general, preferring list initialization is considered good practice by some, because it avoids this parsing ambiguity. Just be aware that it works differently when a type has a constructor taking
std::initializer_list(not a problem for any of the types we're using).Since C++17
You can also initialize the static data member in the class, like:
Note: if all your class does is storing static data members, it should likely just be a
namespace, not aclass.