自定义UniformRandomBitGenerator无法编译

时间:2017-09-08 23:27:45

标签: c++ c++14

我正在尝试围绕C ++头中定义的返回uint_fast32_t的随机数生成源创建一个包装器,这样一个基类就可以存储指向任何RNG的指针(std :: mt19937,std :: default_random等)。这是为了将随机数生成源存储为类成员而不必使该类成为模板类。我提出的解决方案:我定义了一个实现C ++ UniformRandomBitGenerator标准的抽象基类RNGSource。然后,我定义了一个实现RNGSource的模板类RNGSourceWrap,将一些C ++ rng源作为参数,并实现RNGSource函数以将调用转发到存储的底层C ++ rng。代码如下:

#ifndef RANDOM_H
#define RANDOM_H
#include <cstdint>


class RNGSource
{
    public: 
        virtual ~RNGSource() = default;
        typedef uint_fast32_t result_type;
        virtual result_type operator() () = 0;
        virtual result_type min() const = 0;
        virtual result_type max() const = 0;
};


template <class Generator>
class RNGSourceWrap : public RNGSource
{
    public:
        RNGSourceWrap(Generator* rng);

        virtual result_type operator() () override;
        virtual result_type min() const override;
        virtual result_type max() const override;
        Generator* rng() const;

    private:
        Generator* rngVal;
};

template <class Generator>
RNGSourceWrap<Generator>::RNGSourceWrap(Generator* rng)
    : rngVal(rng) { }

template <class Generator>
typename RNGSourceWrap<Generator>::result_type 
RNGSourceWrap<Generator>::operator() () { return rngVal->operator() (); }

template <class Generator>
typename RNGSourceWrap<Generator>::result_type 
RNGSourceWrap<Generator>::min() const { return rngVal->min(); }

template <class Generator>
typename RNGSourceWrap<Generator>::result_type 
RNGSourceWrap<Generator>::max() const { return rngVal->max(); }

template <class Generator>
Generator* RNGSourceWrap<Generator>::rng() const { return rngVal; }
#endif // RANDOM_H

我写了一个小主要来测试功能性:

#include "random.h"
#include <random>
#include <iostream>

int main()
{
    std::mt19937 rng;

    RNGSource* rngWrap = new RNGSourceWrap<std::mt19937>(&rng);
    std::uniform_int_distribution<int> distro(1, 6);
    int myInt = distro(*rngWrap);

    std::cout << myInt << std::endl;

    delete rngWrap;

    return 0;
}

使用c ++ 14标准编译Linux g ++和MinGW时,此代码编译并运行时没有任何警告或错误。但是,使用MSVC编译时会产生错误(使用Visual Studio 2017社区):

1>------ Build started: Project: RNGTestApp, Configuration: Debug Win32 ------
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility(3479): error C2352: 'RNGSource::max': illegal call of non-static member function
1>[PATH REMOVED]\visual studio 2017\projects\rngtestapp\random.h(14): note: see declaration of 'RNGSource::max'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility(3476): note: while compiling class template member function 'std::_Rng_from_urng<unsigned int,_Engine>::_Rng_from_urng(_Urng &)'
1>        with
1>        [
1>            _Engine=RNGSource,
1>            _Urng=RNGSource
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\random(2353): note: see reference to function template instantiation 'std::_Rng_from_urng<unsigned int,_Engine>::_Rng_from_urng(_Urng &)' being compiled
1>        with
1>        [
1>            _Engine=RNGSource,
1>            _Urng=RNGSource
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\random(2353): note: see reference to class template instantiation 'std::_Rng_from_urng<unsigned int,_Engine>' being compiled
1>        with
1>        [
1>            _Engine=RNGSource
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\random(2312): note: see reference to function template instantiation 'int std::uniform_int<_Ty>::_Eval<_Engine>(_Engine &,_Ty,_Ty) const' being compiled
1>        with
1>        [
1>            _Ty=int,
1>            _Engine=RNGSource
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\random(2312): note: see reference to function template instantiation 'int std::uniform_int<_Ty>::_Eval<_Engine>(_Engine &,_Ty,_Ty) const' being compiled
1>        with
1>        [
1>            _Ty=int,
1>            _Engine=RNGSource
1>        ]
1>[PATH REMOVED]\visual studio 2017\projects\rngtestapp\main.cpp(11): note: see reference to function template instantiation 'int std::uniform_int<_Ty>::operator ()<RNGSource>(_Engine &) const' being compiled
1>        with
1>        [
1>            _Ty=int,
1>            _Engine=RNGSource
1>        ]
1>[PATH REMOVED]\visual studio 2017\projects\rngtestapp\main.cpp(11): note: see reference to function template instantiation 'int std::uniform_int<_Ty>::operator ()<RNGSource>(_Engine &) const' being compiled
1>        with
1>        [
1>            _Ty=int,
1>            _Engine=RNGSource
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility(3479): error C2352: 'RNGSource::min': illegal call of non-static member function
1>[PATH REMOVED]\visual studio 2017\projects\rngtestapp\random.h(13): note: see declaration of 'RNGSource::min'
1>Done building project "RNGTestApp.vcxproj" -- FAILED.

鉴于使用Linux g ++和MinGW进行编译,并且看起来有资格实现UniformRandomBitGenerator标准,这是编译器错误吗?

1 个答案:

答案 0 :(得分:0)

在我看来,你理解UniformRandomBitGenerator概念的措辞是错误的。 result_typemaxmin都需要定义static,您的实施无法做到。虽然配方中没有使用“静态”这个词,但要仔细研究表达式的写法。

在你的包装中你可以相应地包装:

template <class Generator>
typename RNGSourceWrap<Generator>::result_type 
RNGSourceWrap<Generator>::min() const { return Generator::min(); }

...并声明各自的方法是静态的。但是,如果有人打电话给RNGSource::min(),那将无效。