Last active
August 29, 2015 13:57
-
-
Save congdanhqx-zz/9621166 to your computer and use it in GitHub Desktop.
Example for template, callback function
This file contains 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
template<class Container> | |
void doWork(Container& container) | |
{ | |
Container::iterator it; | |
Container::iterator * it_ptr; | |
} | |
struct Person | |
{ | |
static int iterator; | |
}; | |
int it_ptr = 1; | |
int main() | |
{ | |
doWork(vector<int>{1,2,3,4,5}); | |
doWork(Person()); | |
return 0; | |
} |
This file contains 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
int doWork(long l, int i, std::string str) | |
{ | |
// do work here. | |
} | |
bool isWorking(int state) | |
{ | |
// check here | |
} | |
typedef int (*func_ptr_type)(long, int, std::string); | |
int main() | |
{ | |
func_ptr_type fp = doWork; | |
fp(1, 1, "abcd"); | |
bool (*fp2)(int) = isWorking; | |
fp2(1); | |
return 0; | |
} |
This file contains 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
struct Comparer | |
{ | |
int operator()(int left, int right) | |
{ | |
return left - right; | |
} | |
}; | |
int main() | |
{ | |
Comparer comparer; | |
cout << comparer(2, 1); | |
return 0; | |
} |
This file contains 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
template< class T > | |
class CCounter | |
{ | |
public: | |
bool CanPerformAction() { | |
return static_cast<T*>(this)->canPerformActionImpl(); | |
} | |
}; | |
class D1: public CCounter< D1 > | |
{ | |
friend class CCounter< D1 >; | |
private: | |
bool canPerformActionImpl() | |
{ | |
return true; | |
} | |
}; | |
class D2: public CCounter< D2 > | |
{ | |
friend class CCounter< D2 >; | |
private: | |
bool canPerformActionImpl() | |
{ | |
return false; | |
} | |
}; |
This file contains 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
template<typename T> | |
struct S { | |
template<typename U> void foo(){} | |
}; | |
template<typename T> | |
void bar() | |
{ | |
S<T> s; | |
s.foo<T>(); // error: < parsed as less than operator | |
s.template foo<T>(); // OK | |
} | |
template<typename T> | |
class Foo | |
{ | |
template<typename U> void foo(){} | |
template<typename U, typename V> | |
void bar(U) | |
{ | |
S<T> s; | |
s.foo<U>(); // error: < parsed as less than operator | |
s.template foo<U>(); // OK | |
T::template foo<V>(); // OK | |
this->template foo<U>(); // OK | |
typename T::template iterator<int>::value_type v; | |
} | |
} |
This file contains 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
/* | |
* Copy from http://coliru.stacked-crooked.com/view?id=5a098714e58041fb | |
* which introduced at http://en.cppreference.com/w/cpp/language/template_specialization | |
*/ | |
#include <iostream> | |
template<typename T> // primary template | |
struct is_void : std::false_type | |
{ | |
}; | |
template<> // explicit specialization for T = void | |
struct is_void<void> : std::true_type | |
{ | |
}; | |
int main() | |
{ | |
// for any type T other than void, the | |
// class is derived from false_type | |
std::cout << is_void<char>::value << '\n'; | |
// but when T is void, the class is derived | |
// from true_type | |
std::cout << is_void<void>::value << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment