C++ Logo

std-discussion

Advanced search

Re: The standard does not specify when to supply default arguments as template arguments

From: jim x <xmh970252187_at_[hidden]>
Date: Fri, 17 Jul 2020 21:58:09 +0800
Peter, Thanks for your response. I'm also confused at the deduction,
My reason is, according to the [temp.deduct.call] rule, that is:
>Template argument deduction is done by comparing each function template parameter type (call it P) that contains template-parameters that participate in template argument deduction with the type of the corresponding argument of the call (call it A) as described below.
As you said, you mean the parameter of function template "func" is
just "Test<T>", Right? So , within this parameter type, only `T` that
participate in template argument deduction. The type of corresponding
function argument is `Test<int,char>`. So, I think the corresponding
position template argument related to the template parameter would be
int, namely T = int, then the instantiation rule would be applied to
default argument and the result would be `Test<int,int>`, It violates
"In general, the deduction process attempts to find template argument
values that will make the deduced A identical to A (after the type A
is transformed as described above).", So I think the compiler
shouldn't report the value of `T` is conflicting instead the type of
parameter and the type of argument are not matched.

Peter Sommerlad (C++) <peter.cpp_at_[hidden]> 于2020年7月17日周五 下午6:40写道:
>
> Jim,
>
> First, if the standard were wrong in that area, multiple implementors
> would have complained decades ago and filed defect reports. They do, for
> example, with the text of the C++20 standard when implementing new
> features uncovers holes in the specification
>
> Second, in your example below, func takes one template argument, that
> could be deduced from a single parameter template T. However, you are
> using a two parameter template as its argument when calling func.
> Therefore, the deduction of funs's T does not work and you get an error
> message. Calling func(Test<int>{}) should compile imho (not checked),
> because func's T get deduced as int and Test's U get defaulted to int as
> well. It is like you want to fit a square peg in a round hole that would
> not fit.
>
> I suggest not using the ISO standard text as a basis for learning C++
> templates. For templates, the excellent book by Daveed Vandervorde and
> Nico Josuttis (2nd edition) is what I would look at first.
>
> Regards
> Peter.
>
> jim x via Std-Discussion wrote on 17.07.20 05:06:
> > template<typename T, typename U = T>
> > struct Test{};
> >
> > template<typename T>
> > void func(Test<T> /*#1*/){
> > }
> >
> > int main(){
> > func(Test<int,char>{});
> > }
> >
> > For this case, the report error is "deduced conflicting types for
> > parameter 'T' ('int' and 'char')", please see
> > https://godbolt.org/z/MnWGYq, It sounds like the function parameter
> > would be the form as Test<T,T>, then deduced Test<T,T> from Test<int,
> > char>, then T will have two different type values. So, I wonder that
> > Does the compiler view Test<T> at #1 as Test<T,T>. At this point,
> > there's no instantiation happening, hence , how does the rule "When a
> > simple-template-id does not name a function, a default
> > template-argument is implicitly instantiated when the value of that
> > default argument is needed." applies to this case. In my mind, the
> > default argument is required at #1, but there shall be no
> > instantiation.
> >
>
>
> --
> Peter Sommerlad
>
> Better Software: Consulting, Training, Reviews
> Modern, Safe & Agile C++
>
> peter.cpp_at_[hidden]
> +41 79 432 23 32

Received on 2020-07-17 09:01:38