简体   繁体   English

带有布尔字段默认初始化的结构?

[英]Struct with boolean field default initialization?

I have the following use case, a struct with some boolean and int variables我有以下用例,一个带有一些 boolean 和 int 变量的结构

struct a {

    int field1;
    bool field2;
    bool field3;

};

I am refactoring this code, and writing a constructor for the struct , the problem is the default initialization of fields.我正在重构这段代码,并为 struct 编写一个构造函数,问题是字段的默认初始化。

I am not criticizing any language construct here, but ideally I would want null to be part of the language itself我不是在这里批评任何语言结构,但理想情况下我希望 null 成为语言本身的一部分

i mean I should be able to define for struct a as我的意思是我应该能够将 struct a 定义为

a : field1(null.int), field2(null.bool), field3(null.bool) {}

C++ does not allow it since null.int or null.bool are not defined. C++ 不允许这样做,因为没有定义 null.int 或 null.bool。 Is the only way to do in C++ is在 C++ 中做的唯一方法是

a: field1(-1), field2(false), field3(false) {}

You can do你可以做

struct a {
    a():field1(), field2(), field3() { }
    int field1;
    bool field2;
    bool field3;
};

And all fields will be zero and false respectively.所有字段将分别为零和假。 If you want to say that the fields have an indeterminate value, i'm afraid you have to use other techniques.如果你想说这些字段有一个不确定的值,恐怕你必须使用其他技术。 One is to use boost::optional :一种是使用boost::optional

struct a {
    a():field1(int()), field2(bool()) { }
    optional<int> field1;
    optional<bool> field2;
    optional<bool> field3;
};

Leaves field3 indeterminate.叶 field3 不确定。 Access the values with *field_name .使用*field_name访问值。 Test for a none value with field == boost::none or if(field) { ... } .使用field == boost::noneif(field) { ... }测试 none 值。

If you're looking for a type with values {true, false, null}, that type is not bool.如果您正在寻找值为 {true, false, null} 的类型,则该类型不是 bool。 However, boost::optional<bool> is such a type.然而, boost::optional<bool>就是这样一种类型。 In the same way, boost::optional<int> can hold any int, or no int at all.以同样的方式, boost::optional<int>可以保存任何 int,或者根本没有 int。

[edit] Or std::optional , since C++17. [编辑] 或std::optional ,因为 C++17。

A boolean has two states.一个布尔值有两种状态。 That's what makes it a boolean.这就是使它成为布尔值的原因。 So, in any strongly typed language, a boolean is either true or false.因此,在任何强类型语言中,布尔值要么是真要么是假。

An integer in c/c++ (and java) is the direct binary representation of a number. c/c++(和java)中的整数是数字的直接二进制表示。 You can assign one value of that number to mean "this number has no value", but this does not make sense in all situations - and if the language were to take that into account, every math operation would have to be preceded by a check - it would really slow things down.您可以为该数字指定一个值来表示“该数字没有价值”,但这在所有情况下都没有意义 - 如果语言要考虑到这一点,则每个数学运算都必须先进行检查- 它真的会减慢速度。

Bottom line: If you want a weak type system, use another language.底线:如果您想要弱类型系统,请使用另一种语言。

You seem to want to be able to say the fields are in an undefined state.您似乎希望能够说字段处于未定义状态。

This goes against the principles of a strongly types language (such as C++) so you are out of luck.这违背了强类型语言(如 C++)的原则,所以你很不走运。
If you want to check if something has been defined of not you need to explicitly track it yourself.如果你想检查是否已经定义了某些东西,你需要自己明确地跟踪它。 You could potentially use pointers to solve your problem but I don't personally think that would be a good idea.您可能会使用指针来解决您的问题,但我个人认为这不是一个好主意。

Maybe if you tried to explain the real problem you are trying to solve we could provide better advice.也许如果你试图解释你试图解决的真正问题,我们可以提供更好的建议。

It's the c++ way not to pay for anything you don't use and default initialization and the ability to have uninitialized bool is something that many people won't need.这是 C++ 的方式,不为您不使用的任何东西付费,默认初始化和具有未初始化 bool 的能力是许多人不需要的。

If you really want this kind of behavior you can build a nullable version of the type you are working with.如果您真的想要这种行为,您可以构建您正在使用的类型的可为空版本。 Something along the lines:沿线的东西:

class NullBool {
  bool m_null;
  bool m_value;
  public:
  NullBool() : m_null(true) {}
  NullBool(bool value) : m_null(false), m_value(value) {}
  void isNull() const {
    return m_null;
  }
  void value() const {
    return m_value;
  }
  ...
  // lots of operations
  ...
};

This should probably be built as a template so it works for more types out of the box.这可能应该构建为模板,以便它适用于更多类型的开箱即用。 And since it's your own type you can easily make the default constructor to make it null by default.而且由于它是您自己的类型,因此您可以轻松地将默认构造函数设置为默认情况下为 null。

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

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