Non Deduced context for a non type parameter

Ashish Daggubatti

I am reading C++ Templates (2nd edition) and this is a snippet from the book:

template<typename... Ts, int N>
void f(double (&)[N+1], Ts... ps) {return;}

It is specified in the book that the declaration above is useless because N cannot be specified or deduced.

I am trying to understand why something like the following is an error:

double arr[2];
f<int, double, 1>(arr, 1, 2.0);

When I compile the snippet above, I get the error that there is no matching function for call to f.

This compiles fine

template<typename... Ts, typename T> 

void func(T value){}; 
func(1); 

even though I have an additional parameter after a parameter pack.

Why does my specifying of the template arguments explicitly not match the arguments provided? Please help me understand this.

Nelfeal

The fact that N cannot be deduced has nothing to do with the parameter pack. This doesn't compile either because N cannot be deduced.

template<int N>
void f(double (&)[N+1]) {}

int main() {
    double arr[2];
    f(arr);
}

From cppreference (Non-deduced contexts):

In the following cases, the types, templates, and non-type values that are used to compose P do not participate in template argument deduction, but instead use the template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.
[...]
3) A non-type template argument or an array bound in which a subexpression references a template parameter

The fact that N cannot be specified has a different cause: the standard says that you simply cannot specify a parameter placed after a parameter pack.

From [temp.param]:

A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list of the function template or has a default argument (14.8.2). [Example:

template<class T1 = int, class T2> class B;    // error

// U can be neither deduced from the parameter-type-list nor specified
template<class... T, class... U> void f() { }  // error
template<class... T, class U> void g() { }     // error

—end example ]

(see this question from which I got the quote)

Your other example works because T can be deduced from the parameters.

template<typename... Ts, typename T> 
void func(T value){}; 
func(1); // 1 is used to deduce T

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Force non-deduced context - type_identity etc

Binding a non-const lvalue reference to a deduced non-type template parameter

Is placeholder for the deduced class type of non-type template parameter a C++20 feature?

ADL fails (or not done?) for function with additional (non deduced) template parameter

Non type template parameter

Overloaded function template disambiguation with `std::enable_if` and non-deduced context

Why is using base class definitions in non-deduced context not permitted, and how to get around this?

just returning non type parameter

Is default template template parameter value deduced context?

Instantiation of a Non-static Class(context) in a Static method with a Constructor taking type Object as parameter

Pass on already deduced type as parameter type for a callback

Meaning of "deduced A" in the context of type deduction from a call

Can a non-type template argument whose type is specified with a placeholder for a deduced class type be passed to another template in C++2a?

How to omit perfect forwarding for deduced parameter type?

non type template parameter pack expansion

pointer non-type template parameter

Partial specialization of non-type parameter

Non-type variadic template parameter

Why is it called "non-type" template parameter?

Non-type reference parameter/argument

Template function dependent on non-type parameter

Template non-type parameter deduction

Template non-type parameter with different types

template non-type template parameter

Passing non-type parameter agument for templates

Integer sequence as a non-type template parameter

Alias Template with non-type template parameter

Non-type template parameter dependent on a default template type parameter

Can C++17's deduced `auto` non-type `template` parameters pattern-match templates with explicit non-type parameters?