Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it guaranteed that template template parameter invoke user provided deduction guides

Consider an example:

#include <type_traits>
#include <string>

template <template <class> class TT> //#1
struct Foo {
   static void foo() {
      static_assert(std::is_same_v<decltype(TT("abc")), TT<std::string>>);
   }
};

template <class T>
struct Bar {
    Bar(T) {}
};

template <class T>
Bar(T) -> Bar<std::string>; //#2

int main() {
    Foo<Bar>::foo();
}

[clang] as well as [gcc] both seem to use user provided deduction guides (#2) when deducing the template parameter of template template parameter (#1). Is it a standard compliant feature?

like image 778
W.F. Avatar asked Oct 06 '17 16:10

W.F.


People also ask

Why do we use template template parameter?

Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.

What is template argument deduction?

Template argument deduction is used when selecting user-defined conversion function template arguments. A is the type that is required as the result of the conversion. P is the return type of the conversion function template.

Can we pass Nontype parameters to templates?

Template classes and functions can make use of another kind of template parameter known as a non-type parameter. 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.

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)


1 Answers

Yes, this is standard compliant.

According to [dcl.type.simple]/2:

A type-specifier of the form typenameoptnested-name-specifieropttemplate-name is a placeholder for a deduced class type ([dcl.type.class.deduct]). The template-name shall name a class template that is not an injected-class-name.

And [temp.param]/3:

A type-parameter whose identifier does not follow an ellipsis defines its identifier to be a typedef-name (if declared without template) or template-name (if declared with template) in the scope of the template declaration.

TT is a type-parameter declared with template, which makes it a template-name and hence a placeholder for a deduced class type. All the usual rules apply just fine.

like image 54
Barry Avatar answered Oct 06 '22 00:10

Barry