What does -> after a function prototype mean?

前端 未结 1 960
闹比i
闹比i 2020-12-30 04:41

What is happening in this code? Is so confusing.

#include 

struct check
{
   template 
   auto foo() -> decltype(std::de         


        
相关标签:
1条回答
  • 2020-12-30 05:31

    Let's go through bit by bit.

    auto foo() -> decltype(std::declval<T>().value, void())
    

    This is a trailing return type. It's allowed to use parameters, but here that isn't necessary. I'd guess it's written like that to be clearer. decltype finds the type of the expression inside, but that expression is not actually evaluated. std::declval is used for creating an instance of the type passed to it. The comma operator is used here to make the overall return type void, since the comma operator evaluates the left side, throws it away, evaluates the right side, and returns that.

    The first part creates a sort of SFINAE (though I've never seen it used like this). For example, if you had an overload of foo that did the same with value2 instead of value, there would be no ambiguity of which to call. See here for what I mean. Compare it to this one, which just has a return type of void and causes errors.

    static_assert(T{}.value == 10, "Incorrect value");
    

    This line makes sure a value-initialized instance of T has its value member have a value of 10. If it doesn't, a compiler error with that text is generated.

    } var;
    

    This is just a global object of that class to use.

    struct apple
    {
       int value{10};
    };
    

    This is a sample class to test it with. It has a value member and that member is 10 in a value-initialized instance (default-initialized as well).

    var.foo<apple>();
    

    This just calls the function.

    0 讨论(0)
提交回复
热议问题