C++ Logo

std-discussion

Advanced search

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

From: Peter Sommerlad (C++) <"Peter>
Date: Fri, 17 Jul 2020 12:40:38 +0200
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 05:43:59