Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "template<class T> using owner = T;"?

Below is excerpted from gsl.h of Microsoft's gsl library (https://github.com/microsoft/gsl):

namespace gsl
{
    //
    // GSL.owner: ownership pointers 
    //
    using std::unique_ptr;
    using std::shared_ptr;

    template<class T>
    using owner = T;
    ...
};

I cannot understand what the following alias template means:

template<class T>
using owner = T;

Any explanations?

like image 662
xmllmx Avatar asked Jul 19 '16 03:07

xmllmx


People also ask

What does class template mean?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template. is a template used to generate template classes.

What is the use of template class?

Definition. As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.

What is class template example?

Class Template: We can define a template for a class. For example, a class template can be created for the array class that can accept the array of various types such as int array, float array or double array.

What is template class and template function?

Function templates. Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.


2 Answers

It means that for every T, owner<T> is an alias for T.

like image 141
Brian Bi Avatar answered Sep 20 '22 01:09

Brian Bi


It can be used as annotation to show which pointers are 'owner' ie:

Example of non owning raw pointer

template<typename T>
class X2 {
    // ...
public:
    owner<T*> p;  // OK: p is owning
    T* q;         // OK: q is not owning
};
like image 39
arenard Avatar answered Sep 22 '22 01:09

arenard