STL learning notes (3) function templates

Source: Internet
Author: User

Function templates cannot be partially customized

The Type compiler of function template parameters can be inferred and omitted, but the return value type must be written.

# Include <iostream> # include <typeinfo> using namespace STD; Template <typename T> class type {public: static string name () {return typeid (t ). name () ;}}; template <> class type <char >{public: static string name () {return "char ";}}; template <> class type <bool> {public: static string name () {return "bool" ;}}; template <> class type <double> {public: static string name () {return "double" ;}}; template <> class type <int> {public: static string name () {return "int ";}}; template <typename T> class type <t *> {public: static string name () {return type <t >:: name () + "Pointer ";}}; // function template <typename T> string type (t) {return type <t>: Name ();} int main () {// function template advantages: cout <type (123) <Endl; cout <type (45.6) <Endl; cout <type () <Endl; cout <"-------------------------" <Endl; int A = 10; char B = 'K'; cout <type (A * 1.0) <Endl; cout <type (a <B) <Endl; cout <type (& A) <Endl ;}

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.