Created
October 29, 2015 08:10
-
-
Save autosquid/bda6be681239b0eab100 to your computer and use it in GitHub Desktop.
A trick for partial specialization of function template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| partial specialization of function template is under consideration of C++ Standards Committee | |
| But before that, we need some tricks | |
| */ | |
| #include "boost/format.hpp" | |
| namespace bamutils{ | |
| template <typename prompt_type, typename content_type> | |
| struct myinfo_imp | |
| { | |
| void operator()(const prompt_type& prompt, const content_type& content){ | |
| std::cout<<prompt<<" : "<< content<<std::endl; | |
| } | |
| }; | |
| template <typename prompt_type> | |
| struct myinfo_imp<prompt_type, boost::format> | |
| { | |
| void operator ()(const prompt_type& prompt, const boost::format& content){ | |
| std::cout<<prompt<<" : "<< content.str()<<std::endl; | |
| } | |
| }; | |
| template <typename content_type> | |
| struct myinfo_imp<boost::format, content_type> | |
| { | |
| void operator() (const boost::format& prompt, const content_type& content){ | |
| std::cout<<prompt.str()<<" : "<< content<<std::endl; | |
| } | |
| }; | |
| template <> | |
| struct myinfo_imp<boost::format, boost::format> | |
| { | |
| void operator() (const boost::format& prompt, const boost::format& content){ | |
| std::cout<<prompt.str()<<" : "<< content.str()<<std::endl; | |
| } | |
| }; | |
| template <typename prompt_type, typename content_type> | |
| void myinfo(const prompt_type& prompt, const content_type& content){ | |
| myinfo_imp<prompt_type, content_type> imp; | |
| imp(prompt, content); | |
| } | |
| template <typename string_type> | |
| void myinfo(const string_type& info){ | |
| std::cout<<info<<std::endl; | |
| } | |
| template<> | |
| void myinfo(const boost::format& info){ | |
| std::cout<<info.str()<<std::endl; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment