[random] example code fails inside struct

struct X { boost::mt19937 rng; boost::uniform_int<> million(1,1000000); // VC++ 10 error C2059: syntax error : 'constant' boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random_value(rng, million); }; Why does the example code from the docs fail to compile when placed inside a struct or class? What am I missing? --Beman

Beman Dawes wrote:
struct X { boost::mt19937 rng; boost::uniform_int<> million(1,1000000); // VC++ 10 error C2059: syntax error : 'constant' boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random_value(rng, million); };
Why does the example code from the docs fail to compile when placed inside a struct or class? What am I missing?
You can't initialize a class data member like that. boost::uniform_int<> million(1,1000000); is valid in a function body or in namespace-scope, but not in class scope. In C++11 I think you could do this however: boost::uniform_int<> million{1,1000000};
--Beman
HTH, Gevorg

On Sat, Apr 16, 2011 at 6:54 AM, Gevorg Voskanyan <v_gevorg@yahoo.com> wrote:
Beman Dawes wrote:
struct X { boost::mt19937 rng; boost::uniform_int<> million(1,1000000); // VC++ 10 error C2059: syntax error : 'constant' boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random_value(rng, million); };
Why does the example code from the docs fail to compile when placed inside a struct or class? What am I missing?
You can't initialize a class data member like that. boost::uniform_int<> million(1,1000000); is valid in a function body or in namespace-scope, but not in class scope...
Doesn't that create a usage problem because there aren't alternative constructors that do work at class scope? --Beman

Beman Dawes wrote:
You can't initialize a class data member like that. boost::uniform_int<> million(1,1000000); is valid in a function body or in namespace-scope, but not in class scope...
Doesn't that create a usage problem because there aren't alternative constructors that do work at class scope?
What do you mean? If I guess what you're asking here correctly, then that is a limitation in C++03 addressed in C++11. Here's the proposal for that: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2756.htm It still won't make the code to compile as you have written it, but with slight changes it will: struct X { boost::mt19937 rng; boost::uniform_int<> million{1,1000000}; // or million = boost::uniform_int<>(1, 1000000); boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random_value{rng, million}; }; Please let me know if that info wasn't what you were looking for. HTH, Gevorg

Message du 16/04/11 12:46 De : "Beman Dawes" A : "Boost Developers List" Copie à : Objet : [boost] [random] example code fails inside struct
struct X { boost::mt19937 rng; boost::uniform_int<> million(1,1000000); // VC++ 10 error C2059: syntax error : 'constant' boost::variate_generator > random_value(rng, million); };
Why does the example code from the docs fail to compile when placed inside a struct or class? What am I missing?
I guess because you are initializing the fields in the declaration. I'm missing something? Best, Vicente
participants (3)
-
Beman Dawes
-
Gevorg Voskanyan
-
Vicente BOTET