Computer Science, asked by ptasha552, 1 year ago

What is meant by a template parameter? It can be used to pass a type as argument it can be used to specify its base class it can be used to evaluate a type. None?

Answers

Answered by kashu77
0

╔═❤️══════════════╗

...HERE GØΣS UR ANS...

╚══════════════❤️═╝

Template parameters and template arguments

C++ C++ language Templates

Every template is parametrized by one or more template parameters, indicated in the parameter-list of the template declaration syntax:

template < parameter-list > declaration (1)

Each parameter in parameter-list may be:

a non-type template parameter;

a type template parameter;

a template template parameter.

Non-type template parameter

type name(optional) (1)

type name(optional) = default (2)

type ... name(optional) (3) (since C++11)

placeholder name (4) (since C++17)

1) A non-type template parameter with an optional name.

2) A non-type template parameter with an optional name and a default value.

3) A non-type template parameter pack with an optional name.

4) A non-type template parameter with a placeholder type. placeholder may be any type that includes the placeholder auto (such as plain auto, auto ** or auto &), a placeholder for a deduced class type (since C++20), or decltype(auto).

type is one of the following types (optionally cv-qualified, the qualifiers are ignored):

lvalue reference type (to object or to function);

std::nullptr_t (since C++11);

an integral type;

a pointer type (to object or to function);

a pointer to member type (to member object or to member function);

an enumeration type.

(until C++20)

a type that

satisfies LiteralType, and

has strong structural equality (see below).

This includes integral types, pointer types, pointer to member type, std::nullptr_t, as well as enumeration types with no custom operator<=> overload, but excludes floating-point types.

A type has strong structural equality if, for a glvalue x of type const T,

if T is a non-class type

x <=> x is a valid expression of type std::strong_ordering or std::strong_equality;

if T is a class type

operator== is defined as defaulted in the definition of T, and

x==x is well-formed when contextually converted to bool, and

all of T's base class subobjects and non-static data members have strong structural equality, recursively, and

T has no mutable or volatile subobjects.

(since C++20)

Array and function types may be written in a template declaration, but they are automatically replaced by pointer to object and pointer to function as appropriate.

When the name of a non-type template parameter is used in an expression within the body of the class template, it is an unmodifiable prvalue unless its type was an lvalue reference type, or unless its type is a class type (since C++20).

A template parameter of the form class Foo is not an unnamed non-type template parameter of type Foo, even if otherwise class Foo is an elaborated type specifier and class Foo x; declares x to be of type Foo.

The type of a non-type template parameter may be deduced if it includes a placeholder type (auto, a placeholder for a deduced class type (since C++20), or decltype(auto)). The deduction is performed as if by deducing the type of the variable x in the invented declaration T x = template-argument;, where T is the declared type of the template parameter. If the deduced type is not permitted for a non-type template parameter, the program is ill-formed.

template<auto n> struct B { /* ... */ };

B<5> b1; // OK: non-type template parameter type is int

B<'a'> b2; // OK: non-type template parameter type is char

B<2.5> b3; // error: non-type template parameter type cannot be double

For non-type template parameter packs whose type uses a placeholder type, the type is independently deduced for each template argument and need not match:

template<auto...> struct C {};

C<'C', 0, 2L, nullptr> x; // OK

(since C++17)

An identifier that names a non-type template parameter of class type T denotes a static storage duration object of type const T, called a template parameter object, whose value is that of the corresponding template argument after it has been converted to the type of the template parameter. All such template parameters in the program of the same type with the same value denote the same template parameter object.

struct A { friend bool operator==(const A&, const A&) = default; };

template<A a> void f() {

&a; // OK

const A& ra = a, &rb = a; // Both bound to the same template parameter object

assert(&ra == &rb); // passes

}

(since C++20)

Type template parameter

Similar questions