简体   繁体   English

是否可以将类型未知的变量声明为类成员变量?

[英]Is it possible to declare a variable of unknown type as a class member variable?

So I've been interested by the mersenne_twister engine and what it can do, so I decided to put the few lines of code required to initialize it inside my own class so that i simply have to create an instance of that class and can get any random numbers in any range i want without having to repeat those lines every time I need it. 因此,我对mersenne_twister引擎及其功能感兴趣,因此,我决定将初始化它所需的几行代码放在自己的类中,这样我只需创建该类的实例即可获取任何内容。我想要的任意范围内的随机数,而不必每次都需要重复这些行。

I have suceeded so far but because I want my code to be as portble and efficient as possible I want to use the 64-bit engine depending on the architecture present. 到目前为止,我已经提出了建议,但是因为我希望我的代码尽可能地可移植和高效,所以我希望根据当前的体系结构使用64位引擎。 I would like to avoid the way of using preprocessor macros defined by the compiler as that doesn't seem like the cleanest approach to me and would also require me to use the macros every time i mention the engine in my code. 我想避免使用由编译器定义的预处理器宏的方法,因为这对我来说似乎不是最干净的方法,并且每次我在代码中提及引擎时,都需要我使用这些宏。

My macro for the architecture l looks like this: 我的架构宏如下所示:

#define CPU_ARCH sizeof(nullptr)*8

And I declare the engine in the private space of the class so that i can init it in the constructor like this: 而且我在类的私有空间中声明了引擎,以便可以在构造函数中将其初始化,如下所示:

engine = mt19937(seed);

and use it in my random function like this: 并在我的随机函数中使用它,如下所示:

double Random::giveRnd() {
    return distribution(engine);
}

This looks fine right now but I have yet to find a way to implement both architectures with the same name "engine" in a way that the engine to be used is chosen at startup. 现在看起来不错,但是我还没有找到一种方法来以启动时选择要使用的引擎的方式来实现具有相同名称“ engine”的两个体系结构。

I have attempted the following: 我尝试了以下操作:

  • Using a template to create a variable named engine that later gets 使用模板创建一个名为engine的变量,该变量稍后会被获取
    assigned either mt19337 or mt19337_64 which results in the compiler 分配了mt19337或mt19337_64,从而导致编译器
    complaining that 抱怨

    error: data member 'engine' cannot be a member template 错误:数据成员“引擎”不能为成员模板

with the following implementation: 具有以下实现:

class Random {
      public:
      [...]

      private:
      template<typename T>
      T engine;

      [...]
};
  • Using boost::variant which requires me to tell 使用boost :: variant需要我告诉
    my giveRnd() function which type to use when I use the engine which is not possible since the type is not known at compile time 我的GiveRnd()函数在使用引擎时无法使用哪种类型,因为该类型在编译时未知
  • Not declaring the engine in the header file at all although this results in the giveRnd() function not being able to use the engine because it is not in the same scope. 根本不在头文件中声明引擎,尽管这会导致GiveRnd()函数无法使用该引擎,因为它不在同一范围内。
  • Using preprocessor macros in the header file and then use typeid in 在头文件中使用预处理器宏,然后在其中使用typeid
    the source code to find out which engine was used, which doesn't seem to be possible like this: 源代码以找出使用了哪个引擎,看起来像这样是不可能的:

    if(CPU_ARCH==32) { engine = mt19337(seed) } if(CPU_ARCH == 32){引擎= mt19337(种子)}

    because the compiler doesn't know that the engine will always be 因为编译器不知道引擎将永远是
    32-bit in this case and complains that I cannot use the '=' operator on two different types. 在这种情况下为32位,并且抱怨无法在两种不同类型上使用'='运算符。

Does anyone have an idea on how to make this possible in a atleast somewhat clean way? 有谁知道如何以至少一种干净的方式实现这一目标? Or do I need to fall back on the preprocessor macros? 还是我需要依靠预处理器宏?

You can implement behaviour that depends on CPU_BITS by making a class template that takes CPU_BITS as a template argument, and is specialized for expected values. 通过制作一个以CPU_BITS作为模板参数并且专门用于期望值的类模板,可以实现依赖于CPU_BITS的行为。 For example: 例如:

#include <random>

template<size_t N> struct CpuOpts;
template<> struct CpuOpts<32> { using EngineType = std::mt19937; };
template<> struct CpuOpts<64> { using EngineType = std::mt19937_64; };

enum { CPU_BITS = sizeof(nullptr)*8 };
using CurrentCpuOpts = CpuOpts<CPU_BITS>;

struct Random
{
    CurrentCpuOpts::EngineType engine;
};

int main()
{
    Random r;
    r.engine.seed(123456);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM