Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can auto placeholder be used to deduce function result in non-type template parameter?

Consider simple example:

template <auto(*X)()>
struct Foo {
    decltype(X()) x;
};

int bar();

int main() {
    static_cast<void>(Foo<bar>{});
}

Both [gcc] and [clang] seem to accept the code. Is the code really c++17 compliant? If so is there some other rule that makes the following code ill formed?

template <class T, auto(*X)(T)>
struct Foo {
    decltype(X(0)) x;
};

int bar(int);

int main() {
    static_cast<void>(Foo<int, bar>{});
}

This one makes only [gcc] unhappy.

Error message:

prog.cc: In function 'int main()':
prog.cc:9:35: error: unable to deduce 'auto (*)(T)' from 'bar'
     static_cast<void>(Foo<int, bar>{});
                                   ^
prog.cc:9:35: note:   mismatched types 'T' and 'int'
like image 265
W.F. Avatar asked Jan 27 '18 22:01

W.F.


Video Answer


1 Answers

Yes, auto may be used inside a compound type ([temp.param]/4.6, [dcl.type.auto.deduct]). I believe that gcc is in error in your second example: your explicitly specified T of int is substituted before performing deduction ([temp.deduct]/2.3, /5, and /6, referenced by [dcl.type.auto.deduct]/2.3 and /4).

like image 54
Davis Herring Avatar answered Nov 15 '22 11:11

Davis Herring