C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: The standard does not specify when to supply default arguments as template arguments
From: Peter Sommerlad (C++) (peter.cpp_at_[hidden])
Date: 2020-07-16 10:33:52


Jim,

since func is also a template, the instantiation happens when func is
instantiated, e.g., when it is called. A dependent template is
instantiated when the surrounding template gets instantiated. That is
the reason for the sometimes confusing C++ error messages when a nested
inner template instantiation fails, due to a missing requirement of the
arguments passed down, or a programming error.

Regards
Peter.

jim x via Std-Discussion wrote on 16.07.20 16:36:
> template<typename T, typename U = T>
> struct Test{};
>
> template<typename T>
> void func(Test<T> /*#1*/){
> }
>
> Consider the above code, at the point of #1, Does the template-id
> Test<T> require a template argument for corresponding template parameter
> U? The only quote that mentioned default argument  in the standard is
> the following :
>
> >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. [ Example:
> >template<typename T, typename U = int> struct S { };
> >S* p; // the type of p is S<bool, int>*
> >The default argument for U is instantiated to form the type S<bool,
> int>*.  >— end example ]
>
> However at the point of #1, The specialization of Test<T> is just a part
> of a function template definition, that is, there's no instantiation
> occuring here. So, the above quote seems to be not suitable for this case.
>
> In the standard, there's a quote explicitly specified when to supply
> default arguments as arguments for function call, that is:
>
> > If an initializer-clause is specified in a parameter-declaration this
> initializer-clause is used as a default argument. Default arguments will
> be used in calls where trailing arguments are missing.
>
> So, Is it necessary to formulate a formally terminology to cover this
> case? such as
>
> >A default template-argument is a template-argument ([temp.arg])
> specified after = in a template-parameter. Default arguments will be
> used in template-id where trailing arguments are missing and the
> corresponding template parameter does not participate in template
> argument deduction.
>
>

-- 
Peter Sommerlad
Better Software: Consulting, Training, Reviews
Modern, Safe & Agile C++
peter.cpp_at_[hidden]
+41 79 432 23 32

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups