简体   繁体   中英

Return pointer to a function

using F = int(*(int))[3];

auto *bar(decltype(foo) *a) -> F
{
    return a;
}

auto bar2(decltype(foo)* a) -> F*
{
    return a;
}

Why the first function can not be compiled? I am so confused about whether the asterisk * should stick with the variable/function name or the qualifier?

auto f() -> T

declares a function f that returns a value of type T . The auto here is not a placeholder type. It's just there because that's what the syntax for function declarators with a trailing return type looks like [dcl.fct]/2 . There is no type deduction going on when declaring a function with a trailing return type. A trailing return type is just an alternate syntax for writing an explicit return type that just happens to also involve the keyword auto but in a capacity different from its normal one.

On the other hand, a declarator like this

auto f()

uses auto as a placeholder type, meaning that the return type of the declared function is to be deduced.

This

auto *bar(decltype(foo) *a) -> F

uses auto as a placeholder return type (it adds a * to it which only makes sense when auto is used as a placeholder type) yet it also attempts to slap on a trailing return type at the end. That's just not how this works. You can either use a trailing return type or a placeholder type. Both at the same time doesn't make any sense. You either want a type to be deduced or you want to say what the type should be…

This

auto bar2(decltype(foo)* a) -> F*

works because this declarator just declares a function with a trailing return type of F*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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