Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in this context can a non-type template parameter not be auto?

Tags:

The simplest snippet I managed to get to reproduce the problem is as follows:

#include <variant>

template <auto V>
using ic = std::integral_constant<decltype(V), V>;

enum { shake }; 
int milk(ic<shake>);

template <class...>
struct context {
    template <auto V>
    decltype(milk(ic<V>{})) get() {
        return std::get<decltype(milk(ic<V>{}))>(value);
    }
    std::variant<int> value;
};

int main(){
    context<int> c;
    c.get<shake>();
}

There is something fishy going on here in [clang] as it suggests that:

prog.cc:13:42: error: a non-type template parameter cannot have type 'auto'
        return std::get<decltype(milk(ic<V>{}))>(value);
                                         ^
prog.cc:3:16: note: template parameter is declared here
template <auto V>
               ^
1 error generated.

When we change the ic to an aliased type or use the untemplated version of context everything works as expected. So is it really clang's bug or am I missing something obvious here?

PS. In [gcc] everything works as expected...

like image 505
W.F. Avatar asked Feb 01 '18 12:02

W.F.


People also ask

Which parameter is allowed for non-type template?

Which parameter is legal for non-type template? Explanation: The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.

Can we use non-type parameters as arguments template?

Non-type template arguments are normally used to initialize a class or to specify the sizes of class members. For non-type integral arguments, the instance argument matches the corresponding template parameter as long as the instance argument has a value and sign appropriate to the parameter type.

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Can template parameters have default values?

Template parameters may have default arguments. The set of default template arguments accumulates over all declarations of a given template.


1 Answers

Clang bug as commented by xskxzr

like image 182
Tobi Avatar answered Sep 21 '22 08:09

Tobi