specialization.cpp

来自「C++ sample code, for the book: C++ blac」· C++ 代码 · 共 36 行

CPP
36
字号
#include <iostream>
#include <string>
using namespace std;

template <typename T>
void add_one(T &a);

template <> void add_one(string &s); 

int main()
{
    int int_value = 0;
    string string_value("Hello from C++");

    add_one(int_value);

    cout << "The new int_value = " << int_value << endl;

    add_one(string_value);

    return 0;
}

template <typename T>  
void add_one(T &n)
{
    n++;
}

template <> void add_one(string &s)  
{
    cout << "Sorry, adding one to the string \"" << s << "\" is not defined." << endl;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?