Exception function specification

Exception function specification

最近學到的 template 寫法, template function specification, 可以稍候指明 template 對應某種 class type 時可以用指定的方式進行操作, 這部份比 C# 更複雜也同時更有彈性.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// forward declare
template<class T>
char const * Print_handler(const T* data);
// template function specification
template<>
char const * Print_handler<int>(const int* data) {
auto str = std::to_string(*data);
return str.c_str();
}
template<>
char const * Print_handler<std::string>(const std::string* data) {
return (char*)((*data).c_str());
}
// forward declare template<class T> char const * Print_handler(const T* data); // template function specification template<> char const * Print_handler<int>(const int* data) { auto str = std::to_string(*data); return str.c_str(); } template<> char const * Print_handler<std::string>(const std::string* data) { return (char*)((*data).c_str()); }
// forward declare
template<class T>
char const * Print_handler(const T* data);

// template function specification
template<>
char const * Print_handler<int>(const int* data) {
    auto str = std::to_string(*data);
	return str.c_str();
}

template<>
char const * Print_handler<std::string>(const std::string* data) {
	return (char*)((*data).c_str());
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

*

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料