简体   繁体   English

C ++,从具有引用返回类型的函数返回什么以指示错误

[英]C++, What to return from a function with reference return type to indicate error

Imagine that we have a class 想象一下,我们有一堂课

class cFoo {};

and another one with a vector of cFoo object, like this: 另一个带有cFoo对象的向量,如下所示:

class cContainer
    {
        public:
            const cFoo& getFoo(std::size_t index) const;
        private:
            std::vector<cFoo> fooList_;
    }
    const fCoo& cContainer::getfoo(std::size_t index) const
    {
        if(index < fooList_.size())
            return fooList_[index];
        else
            ??????????
    };

So my question is "What is the best practice in this case?". 所以我的问题是“在这种情况下最佳做法是什么?”。 That is what to put after the else part in place of ????s. 这是在其他部分代替????之后放的东西。 This question is valid for any member function returning a reference (be it const or not). 此问题对于返回引用的任何成员函数(无论是否为const)都有效。 When we don't have anything to return, what should be returning? 当我们没有任何东西可以返回时,应该返回什么?

Obviously returning a local temporary variable is out of question. 显然返回一个本地临时变量是不可能的。 Another solution comes to my mind is to return a static instance of cFoo() most possibly defined and initialized during cFoo class definition/implementation. 我想到的另一个解决方案是返回一个cFoo()的静态实例, cFoo()最有可能在cFoo类定义/实现期间定义和初始化。

Of course we can abstain from returning references in this cases but it might be poor choice performance wise (especially if we lack the goodies like move operator). 当然,在这种情况下,我们可以避免返回引用,但它可能是性能差的选择(特别是如果我们缺少像move运算符这样的好东西)。

What the STL does in this case is throw an exception. STL在这种情况下所做的是抛出异常。

For exemple you can take a look at the std::vector::at member function: 例如,您可以查看std::vector::at成员函数:

http://en.cppreference.com/w/cpp/container/vector/at http://en.cppreference.com/w/cpp/container/vector/at

which throws an std::out_of_range exception if you ask for something out of range 如果你要求超出范围的东西,它会抛出std :: out_of_range异常


Another option is to return either a pointer or its more semanticaly explicit equivalent in this context, an optional reference boost::optional<fCoo&> . 另一种选择是在此上下文中返回指针或其更具语义boost::optional<fCoo&>等价物, 可选引用 boost::optional<fCoo&>

I would advise to go this way if the "out of range" case is not that exceptional and is supposed to happen from time to time. 如果“超出范围”的情况不是那么特殊并且应该不时发生,我建议采用这种方式。

Lastly if you consider that this case should never happen except if it is a developper error, you should use an assertion . 最后,如果你认为这种情况永远不会发生,除非它是一个开发者错误,你应该使用一个断言

If not having something to return is normal for the function, it shouldn't be returning a reference. 如果没有返回的东西对于函数是正常的,它不应该返回引用。 There's nothing wrong with returning a pointer: 返回指针没有错:

const fCoo* cContainer::getfoo(std::size_t index) const
{
    if(index < fooList_.size())
        return fooList_[index];
    else
        return nullptr;
};

If not having anything to return is indicates something exceptional for the function, well then, it should throw an exception: 如果没有任何返回的东西表示该函数的异常 ,那么它应该抛出异常:

const fCoo* cContainer::getfoo(std::size_t index) const
{
    if(index < fooList_.size())
        return fooList_[index];
    else
        throw std::invalid_argument("out of range");
};

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

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