简体   繁体   English

如何将断言放在类初始化列表之前?

[英]How can assertions be placed before a class initialisation list?

Is it possible to assert something about the parameters on a class constructor before the initialisation list is called? 在调用初始化列表之前,是否可以在类构造函数上声明一些有关参数的信息?

class Foo
{
    int m_lower;
    int m_upper;
    unsigned int m_delta;

  public:
    Foo(int t_lower, int t_upper) :
        assert(t_lower < t_upper),  // Assert here, before initialisation of fields.
        m_lower(t_lower),
        m_upper(t_upper),
        m_delta(t_upper - t_lower)
    {
       // Assert could be made here, but m_delta would have underflowed if t_upper < t_lower.  
    }
}

The benefit of an assert before the initialisation list would be that requirements for the initialisation of each field could be made immediately and would not have to be checked potentially multiple times with each initialisation that has the same requirements. 在初始化列表之前进行断言的好处是可以立即提出每个字段的初始化要求,并且不必每次都对具有相同要求的初始化进行多次检查。 Whilst initialisation of m_delta could be made in an init_delta initialisation method, if several values have the same requirement that t_upper > t_lower then the assertion would have to placed in each one (incase a previous assertion is removed). 虽然可以使用init_delta初始化方法进行m_delta的初始化,但是如果多个值对t_upper > t_lower具有相同的要求,则必须将断言放置在每个断言中(如果删除了先前的断言)。 If placed wihtin the construct function itself, then the intialisation of one or more fields may have already failed (with something more dramatic than an underflow). 如果将其与构造函数本身放在一起,则一个或多个字段的初始化可能已经失败了(比下溢更具戏剧性)。

If placed at the top of the initialisation list then the contract is both clear to the user upon inspection and would flag an error before any errors, such as underflow in this case, would occur. 如果放置在初始化列表的顶部,则合同在检查时对用户都是清楚的,并且会在发生任何错误(在这种情况下为下溢)之前标记错误。

The above case is just a simplified example of the issue. 上述情况只是该问题的简化示例。 I know there are better ways to solve the specific issue above (abs() etc). 我知道有更好的方法来解决上述特定问题(abs()等)。

Thanks for your help and advice! 感谢您的帮助和建议!

You can use the comma operator: 您可以使用逗号运算符:

  public:
    Foo(int t_lower, int t_upper) :
        m_lower(assert(t_lower < t_upper), t_lower),
        m_upper(t_upper),
        m_delta(t_upper - t_lower)
    {
        ...
    }

Operands of the comma operator are evaluated left-to-right, and the right value is used as the result. 逗号运算符的操作数从左到右求值,并且将右值用作结果。

Note that the order of initializations is based on the order that the member variables are declared in the class, not the order in the initialization list. 请注意,初始化的顺序基于在类中声明成员变量的顺序,而不是初始化列表中的顺序。 So you should put the assert() call in the initialization of the first member variable. 因此,您应该将assert()调用放在第一个成员变量的初始化中。

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

相关问题 如何锁定使用构造函数和初始化列表的类的成员函数 - how to lock a member function of a class which uses a constructor with an initialisation list 如何连接两个字符串以用作初始化列表中的参数? - How can I concatenate two strings to use as an argument in an initialisation list? 在C ++中的类构造函数中进行列表初始化 - List initialisation in a class constructor in C++ 如何在初始化列表中初始化结构类型? - How to initialise a struct-type in the initialisation list? 如何使用初始化列表在构造函数中进行赋值 - How to make assignments in Constructor using initialisation list 如何修复静态类成员的初始化顺序? - How to fix initialisation order of static class member? 派生类的副本构造函数(C ++)的初始化列表上的基类 - Base class on the initialisation list of a derived class' copy constructor (C++) 在进行其他某些初始化之后,如何初始化类的静态成员? - How can I initialise a static member of a class after certain other initialisation has taken place? 如何“跟踪”缺少的ctor初始化列表参数? - How to “track” missing ctor initialisation list arguments? 派生类构造函数初始化列表中的多重继承和继承的数据成员 - Multiple Inheritance and inherited data member in the derived class constructor initialisation list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM