Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-last default template arguments for function templates

C++11 introduced default template arguments for function templates. See also Default template arguments for function templates.

But reading the C++ standard I could not find that it is legal to define a function template which use default template arguments for the first template argument, but not the other template arguments.

This would be the opposite of how default arguments are handled where all subsequent parameters must have a default argument supplied; or be a function parameter pack.

The difference between default arguments and default template arguments seems strange at a first glance, but allows for constructs as:

template <typename TException = std::exception, typename TObjectBuilder>
auto SwallowExceptions(const TObjectBuilder& rObjectBuilder) -> decltype(rObjectBuilder())
{
   try
   {
      return rObjectBuilder();
   }
   catch (const TException&)
   {
      return decltype(rObjectBuilder())();
   }
}

Is this legal C++ code and where in the standard can this be found?

like image 750
dalle Avatar asked Nov 13 '15 10:11

dalle


People also ask

Can template parameters have default arguments?

You cannot give default arguments to the same template parameters in different declarations in the same scope. The compiler will not allow the following example: template<class T = char> class X; template<class T = char> class X { };

What are non-type parameters for templates?

A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument. A non-type parameter can be any of the following types: An integral type. An enumeration type.

Can we use non-type parameters as argument templates?

A non-type template argument provided within a template argument list is an expression whose value can be determined at compile time. Such arguments must be constant expressions, addresses of functions or objects with external linkage, or addresses of static class members.

Can default argument be used with the template class?

Can default arguments be used with the template class? Explanation: The template class can use default arguments.


1 Answers

I can't see a direct quote which allows it, but it is certainly allowed by omission:

N3337 [temp.param]/11: If a template-parameter of a class template or alias template has a default template-argument, each subsequent template-parameter shall either have a default template-argument supplied or be a template parameter pack. If a template-parameter of a primary class template or alias template is a template parameter pack, it shall be the last template-parameter. A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced or has a default argument (14.8.2).

So this is disallowed for class templates and alias templates, but allowed for function templates as the parameters which follow those with defaults could be deduced from the function arguments.

like image 198
TartanLlama Avatar answered Oct 23 '22 13:10

TartanLlama