Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename container> void mySuperTempalte (const container myCont)
- {
- //do something here
- }
- template <typename container> void mySuperTempalte (const container<std::string> myCont)
- {
- //check type of container
- //do something here
- }
- template<typename container> void mySuperTempalte( const container &myCont )
- {
- // Implement STL container code
- }
- template<> void mySuperTempalte( const container<std::string> &myCont )
- {
- // Implement the string code
- }
- template<typename C> void mySuperTempalte( const std::vector<C> &myCont)
- {
- // check type of container
- // do something here
- }
- template<> void mySuperTempalte<std:string>(const std::string myCont)
- {
- //check type of container
- //do something here
- }
- template<typename C> void mySuperTempalte (std::vector<C> myCont)
- {
- //check type of container
- //do something here
- }
- template<typename C> void mySuperTempalte (std::deque<C> myCont)
- {
- //check type of container
- //do something here
- }
- template <template <typename> class Container>
- void mySuperTemplate(Container<std::string> const& cont) {
- }
- //check type of container
- inline template <typename C> void mySuperTemplate (C const& myCont)
- {
- mySuperTemplateImpl<C, typename C::value_type>(myCont);
- }
- template<typename C> void mySuperTempalte (std::vector<C> myCont)
- {
- //check type of container
- //do something here
- }
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- template <bool B, class T> struct enable_if {};
- template <class T> struct enable_if<true, T> { typedef T type; };
- template <class T, class U> struct is_same { enum { value = false }; };
- template <class T> struct is_same<T,T> { enum { value = true }; };
- namespace detail{
- // our special function, not for strings
- // use ... to make it the least-prefered overload
- template <class Container>
- void SpecialFunction_(const Container& c, ...){
- cout << "invoked SpecialFunction() defaultn";
- }
- // our special function, first overload:
- template <class Container>
- // enable only if it is a container of mutable strings
- typename enable_if<
- is_same<typename Container::value_type, string>::value,
- void
- >::type
- SpecialFunction_(const Container& c, void*){
- cout << "invoked SpecialFunction() for stringsn";
- }
- }
- // wrapper function
- template <class Container>
- void SpecialFunction(const Container& c){
- detail::SpecialFunction_(c, 0);
- }
- int main(){
- vector<int> vi;
- cout << "calling with vector<int>n";
- SpecialFunction(vi);
- vector<string> vs;
- cout << "ncalling with vector<string>n";
- SpecialFunction(vs);
- }
- d:scratch>parameterize.exe calling
- with vector<int> invoked
- SpecialFunction() default
- calling with vector<string> invoked
- SpecialFunction() for strings
- d:scratch>
- enum container_types
- {
- unknown,
- list_container,
- vector_container
- };
- template <typename T>
- struct detect_container_
- {
- enum { type = unknown };
- };
- template <typename V>
- struct detect_container_< std::vector<V> > // specialization
- {
- enum { type = vector_container };
- };
- template <typename V>
- struct detect_container_< std::list<V> >
- {
- enum { type = list_container };
- };
- // Helper function to ease usage
- template <typename T>
- container_types detect_container( T const & )
- {
- return static_cast<container_types>( detect_container_<T>::type );
- }
- int main()
- {
- std::vector<int> v;
- assert( detect_container( v ) == vector_container );
- }
Add Comment
Please, Sign In to add comment