简体   繁体   English

在某些情况下,将auto用作返回类型和返回值nullptr

[英]Using auto as return type and return value nullptr in some cases

If we have method with return type auto , but into the method we will return a new object or nullptr . 如果我们具有返回类型为auto的方法,但是在该方法中,我们将返回一个新对象或nullptr If I correctly understand when we return nullptr , it will create also new object through constructor. 如果我正确理解何时返回nullptr ,它将通过构造函数创建新对象。

Method is With . 方法With

Question is next: Which type will be used in place of auto ? 接下来的问题是:将使用哪种类型代替auto It will depend on type returning by maybe or not: maybe is a function which returns Maybe<T> . 这将取决于类型通过返回maybe与否: maybe是返回的函数Maybe<T> When we call first With returning type is Maybe< Adress > ; 当我们第一次调用With返回类型为Maybe< Adress > second step it may be Maybe< Adress > since is the type of object or Maybe< std::string > - it returns if context is not nullptr . 第二步,可能是Maybe< Adress >因为它是对象的类型,或者Maybe< std::string > -如果context不是nullptr则返回。

struct Address {
    string* house_name = nullptr;
};

struct Person {
    Address* address = nullptr;
};

template <typename T> struct Maybe;

template <typename T> Maybe<T> maybe(T* context)
{
   return Maybe<T>(context);
}

template <typename T>
struct Maybe {
    T* context;

    Maybe(T *context) : context(context) { }

    template <typename TFunc>
    auto With(TFunc evaluator)
    { 
        return context != nullptr ? maybe(evaluator(context)) : nullptr;
    }
 };

 ...

 void print_house_name(Person* p)
 {
    auto z = maybe(p)
    .With([](auto x) { return x->address; })
    .With([](auto x) { return x->house_name; })
    .Do([](auto x) { cout << *x << endl; });
 }


int main()
{
   //print_house_name(nullptr);

   Person p;

   print_house_name(&p); // nothing

}

Which type will be used in place of auto ? 哪种类型将代替auto

The return type of your function is determined by the type of the expression in the single return statement. 函数的返回类型由单个return语句中的表达式类型决定。 In your case the statement is: 您的情况是:

return context != nullptr ? maybe(evaluator(context)) : nullptr;

The returned expression is a ternary operator whose two potential values have different types ( Maybe<C> , for some class C not necessarily T , and nullptr_t ). 返回的表达式是一个三元运算符,其两个潜在值具有不同的类型( Maybe<C> ,对于某些类C不一定是T ,并且为nullptr_t )。 This is only well-formed if one of the types is implicitly convertible to the other. 仅当其中一种类型可以隐式转换为另一种类型时,这种格式才正确。 Normally nullptr_t converts only to/from other pointer types, so let's look at the implicit conversions defined for Maybe (there's only one): 通常, nullptr_t仅与其他指针类型进行转换,因此让我们看一下为Maybe定义的隐式转换(只有一个):

Maybe(T *context) : context(context) { }

A pointer type can be converted to a Maybe . 指针类型可以转换为Maybe So nullptr gets converted to C* which then gets converted to a Maybe<C> object (whose context is null). 因此,将nullptr转换为C* ,然后将其转换为Maybe<C>对象(其上下文为null)。 Again, I'm using C instead of T because this type need not be the same template parameter that is part of the type of *this . 同样,我使用C而不是T因为此类型不必是*this类型的一部分的同一模板参数。 The returned type is the same regardless of the value of context . 无论context的值如何,返回的类型都是相同的。

If you want to see this implicit conversion break, make the conversion to Maybe explicit, as in explicit Maybe(T *context) : context(context) { } . 如果您希望看到这种隐式转换中断,请像explicit Maybe(T *context) : context(context) { }一样,将转换转换为Maybe

auto isn't magical. auto不是魔术。 It it simply a placeholder that your compiler fills in with a concrete, deduced type. 它只是一个占位符,您的编译器将其填充为具体的推断类型。 The end result is exactly the same as if you had written the deduced type manually. 最终结果您手动编写推断类型完全相同

"If I correctly understand when we return nullptr, it will create also new object through constructor". “如果我正确理解何时返回nullptr,它将通过构造函数创建新对象”。 No. Returning nullptr does not call any constructors. 否。返回nullptr不会调用任何构造函数。 It simply returns a null value of the appropriate type. 它只是返回适当类型的空值。 No object is constructed. 没有构造对象。

"Which type will be instead auto" - The return tupe will be whatever the type of what you actually return is . “哪种类型将代替汽车” -返回TUPE将是什么的你实际上会返回类型 And that can only ever be one specific type. 那只能是一种特定的类型。

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

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