explicit specialization 显式指定

//explicit specialization 显式指定
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct msg
{
    string title;
    string content;
};

//非模板函数
void Broadcast(const string& msg);

//模板函数
template <typename T>
void Broadcast(const T& msg);

//explicit specialization 显式指定
template<> void Broadcast<msg>(const msg& msg);    

int main(void)
{
    string simpleMsg = "You just receive one message";
    int msgNum = 73357;
    msg msg;
    msg.title = "Attention";
    msg.content = "Perimeter has been breached, evacuate to section 7.";

    Broadcast(simpleMsg);
    Broadcast(msgNum);
    Broadcast(msg);
    
    cin.get();
    return 0;
}

void Broadcast(const string& msg)
{
    cout << "Notice: " << msg << endl;
}

template <typename T>
void Broadcast(const T& msg)
{
    cout << "No: " << msg << endl;
}

template<> void Broadcast<msg>(const msg& msg)
{
    cout << endl;
    cout << "************************************" << endl;
    cout << "Title: " << msg.title << endl;
    cout << "Content: " << msg.content << endl;
    cout << "************************************" << endl;
}

原文地址:https://www.cnblogs.com/heben/p/9308137.html