Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the compiler deduce auto template parameter unless I add const?

I recently had problem with code like this:

constexpr auto lambda = []{};

template<auto& l>
struct Lambda {};

template<auto& l>
void test(Lambda<l>) {}

int main() {
    test(Lambda<lambda>{});
}

Both clang and GCC tells that it can't deduce l.

However, if I add const there:

//   ----v
template<const auto& l>
void test(Lambda<l>) {}

Then everything works with clang. GCC still fails. What's happening here? Can it not deduce the const itself? Is this a GCC bug for it to not deducing l in both cases?

like image 941
Guillaume Racicot Avatar asked Dec 18 '17 07:12

Guillaume Racicot


1 Answers

Is this a GCC bug for it to not deducing l in both cases?

It is a bug, and for Clang too. For a placeholder type non-type argument, [temp.arg.nontype]/1 says:

If the type of a template-parameter contains a placeholder type, the deduced parameter type is determined from the type of the template-argument by placeholder type deduction. If a deduced parameter type is not permitted for a template-parameter declaration ([temp.param]), the program is ill-formed.

The very same process by which it would deduce here

int main() {
   auto& l = lambda;
}

That l is const reference.

like image 74
StoryTeller - Unslander Monica Avatar answered Nov 13 '22 22:11

StoryTeller - Unslander Monica