C++11 template alias & function template Default template parameters __jquery

Source: Internet
Author: User
Tags aliases
Template alias

In c++98/03, we can define an alias for a type by using the TypeDef keyword, such as the typedef unsigned int uint_t, where we have defined unsigned int type aliases and uint_t, We can replace the uint_t with the unsigned int later, but uint_t is only an alias for the unsigned int, as the following definition is illegal:

typedef unsigned int uint_t;
void  func (unsigned int);
void  func (uint_t);
The Func function above is an illegal function overload, although it is convenient to use typedef to define a type alias, but there are some limitations to the use of TypeDef, such as the typedef cannot redefine the alias of a template.

Consider the following example, we often use in the actual programming in the STL map, our map string type data as the map key, we want to according to string type key map to a string,int,long and other types of data.

typedef std::map<std::string, int> map_t;
//...
typedef STD::MAP < std::string, std::string > map_str;
//...
If you need to map to 10 types of data, we need to use TypeDef to define 10 specific types of aliases, but given that the map's key value is always variable, we can use the typedef+ template to define an alias like the following

Template <typename t>
typedef std::map<std::string, t> map;
Map<int>  map_i;
Map<std::string>  Map_str;
Unfortunately, the above definition cannot be compiled, that is, C + + 98/03 does not support such an operation, but usually through a package class to achieve the above requirements:

Template <typename t>
struct alias_map
{
	typedef std::map<std::string, t> map;
};

Alias_map<int>::map map_t;
Alias_map<int>::map Map_str;

Through the package class method can achieve the above requirements, but a look at the code is that the code is poor readability, is not to define a variable. Also need the whole package to encapsulate, add code not to say, look at all annoying, small lucky is c++11 finally let you can not through this bloated way to achieve this demand. In C++11, a new feature is that you can use a using to define aliases for a template, such as the above requirements, which can be implemented using C++11:

Template <typename t>
using Alias_map = Std::map < std::string, T >;

Alias_map<int>  map_t;
Alias_map<std::string> Map_str;
I don't think it's much more comfortable, it's refreshing. In c++11, the use-key is allowed to define aliases as a template, and the use of the Using keyword and the typedef keyword to define the usage of the generic type alias is actually used, which contains all the functionality of the TypeDef.

typedef unsigned int uint_t;
Using uint_t = unsigned int;

typedef std::map<std::string, int> map_t;
Using map_t = Std::map < std::string, int >;
You can see that the use of the two methods is basically equivalent to the definition of an alias for a common type, the only difference being a defined syntax, which is like an assignment, but when you define a function pointer, the using appears to be slightly more readable, such as:

typedef void (*FUNC) (int, int);
Using Func = void (*) (int, int);
It might seem odd to suddenly look like using a using method to define a function pointer, but when you get used to it, you'll find that using this assignment is more appropriate for the way developers think. An example of a function template defined by a typedef and using method is shown below:

Template<typename t>
struct FUNCST
{
	typedef void (*FUNC) (T, t);

Funcst<int>::func Func_typedef;


Template<typename t>
using func_using = void (*func) (T, T);
Func_using<int> func_using;
You can see the syntax for using the use-definition template alias, simply by adding a list of template arguments based on the normal type alias syntax, by using a use that makes it easy to create a template alias without needing to add a parcel class like c++98/03. But the extra note is that using using or typedef simply defines an alias and does not create new types.


Default template parameters for functions

In c++98/03, class templates are supported by default template parameters, such as:

Template<class T, class u = int, u n= 0>
struct Foo
{
	//
};
However, you do not support the default template parameters for function templates in c++98/03:

Template <typename T = int>//error in c++98/03 default template arguments
void func (void)
{
//...
  }
Now that the limit has been lifted in the c++11, the above definition can be used directly in c++11. In a function template, when all template parameters have default parameters, the call to the function is like a normal function call, but for class end classes, even if all template parameters have a default constructor to be used, it must be instantiated after the template name followed by <>.

The default template parameters for functions in c++11 are also somewhat different from the default parameters of the normal function, which must be written at the end of the argument list, and the template parameters of the function do not have this limitation, so it is very flexible when using the default template parameters and template parameters for automatic derivation. You can specify that some of the parameters in the function are default parameters and the other part is automatically inferred. Like what:

Template <typename r = int, typename u>
R func (U val)
{
	//...
}

int _tmain (int argc, _tchar* argv[])
{
	func (123);
	return 0;
}
However, if you display the parameters of a specified template when you use a function template, because the fill order of the template parameters is Left-to-right, the type returned by a call like the following is a long type:

Func<long> (123); Func return type is a fill type long

Although this detail is simple, it can be easily ignored when multiple default template parameters and multiple template parameters are automatically deduced and interspersed. The use of a number of accidents, it is recommended that when used as far as possible or price the default template parameters are written at the end of the template parameters, and when the default template parameters and automatic parameter derivation are used simultaneously, If the function template cannot deduce the parameter type, the compiler will use the default template parameter, otherwise the parameter type that is automatically inferred will be used. This is consistent with the rules for using the default parameters of a function, which is better understood.


Template aliases and default template parameters are some of the small details in generic programming, the c++11 to c++98/03 some of the details, so the introduction is not much space, mainly in the use of the time if you can add code readability, reduce code redundancy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.