Last active
August 29, 2015 14:23
-
-
Save Slowhand0309/0c63288f432c887b8d54 to your computer and use it in GitHub Desktop.
C++ template samples
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
#include <boost/format.hpp> | |
#include <boost/shared_ptr.hpp> | |
#include <iostream> | |
#include <iterator> | |
#include <string> | |
#include <vector> | |
#include <list> | |
using namespace std; | |
// declare start for 5-1 | |
struct widget { | |
typedef int value_type; | |
}; | |
template <class T> | |
void something(typename T::value_type*) | |
{ | |
cout << "has value_type" << endl; | |
} | |
template <class T> | |
void something(...) | |
{ | |
cout << "other" << endl; | |
} | |
void sample5_1() | |
{ | |
cout << "SFINAE p120 5-1" << endl; | |
something<widget>(0); | |
something<int>(0); | |
} | |
// declare end for 5-1 | |
// declare start for 5-3 | |
template <class T> | |
struct _is_void { | |
static const bool value = false; | |
}; | |
template <> | |
struct _is_void<void> { | |
static const bool value = true; | |
}; | |
void sample5_3() | |
{ | |
cout << "SFINAE p122 5-3" << endl; | |
cout << boolalpha; | |
cout << _is_void<void>::value << endl; | |
cout << _is_void<int>::value << endl; | |
cout << _is_void<char*>::value << endl; | |
} | |
// declare end for 5-3 | |
// declare start for 5-33 | |
// ☆☆☆ constexpr is not available for disabled test ☆☆☆ | |
//template <class T> | |
//class has_iterator { | |
// template <class U> | |
// static constexpr bool check(typename U::iterator*) | |
// { return true; } | |
// | |
// template <class U> | |
// static constexpr bool check(...) | |
// { return false; } | |
// | |
//public: | |
// static const bool vazlue = check<T>(nullptr); | |
//}; | |
// | |
//void sample5_33() | |
//{ | |
// cout << "SFINAE p125 5-33" << endl; | |
// | |
// static_assert( | |
// has_iterator<std::vector<int>>::value, | |
// "vector has iterator."); | |
// | |
// static_assert( | |
// !has_iterator<int>::value, | |
// "int has not iterator."); | |
//} | |
// declare end for 5-33 | |
// declare start for 6-1 | |
struct debug_log { | |
static void print(const string &value) { | |
cout << value << endl; | |
} | |
}; | |
struct release_log { | |
static void print(const string &value) {} | |
}; | |
template <class LogPolicy> | |
struct Hoge { | |
void foo() const { | |
LogPolicy::print("Fatal Error!!!"); | |
} | |
}; | |
// Static member function | |
void sample6_1() | |
{ | |
Hoge<debug_log>().foo(); | |
Hoge<release_log>().foo(); | |
} | |
// 非静的メンバ関数 | |
struct policy6_8 { | |
void foo() | |
{ | |
cout << "policy call foo." << endl; | |
} | |
}; | |
template <class Policy> | |
class widget6_8 { | |
Policy _policy; | |
public: | |
void do_something() | |
{ | |
_policy.foo(); | |
} | |
}; | |
void sample6_8() | |
{ | |
widget6_8<policy6_8> hoge; | |
hoge.do_something(); | |
} | |
// Inheritance | |
class policy6_9 { | |
protected: | |
void foo() { | |
cout << "policy6_9 class call foo." << endl; | |
} | |
public: | |
virtual void abc() { | |
cout << "policy6_9 class call abc." << endl; | |
} | |
void first() { | |
cout << "policy6_9 class call first." << endl; | |
} | |
}; | |
class fakePolicy { | |
public: | |
void hoge() { | |
cout << "fakePolicy class call hoge." << endl; | |
} | |
}; | |
template <class Policy> | |
class widget6_9 : public Policy | |
{ | |
public: | |
void do_something() | |
{ | |
//foo(); | |
} | |
virtual void abc() { | |
cout << "widget6_9 class call abc." << endl; | |
} | |
}; | |
void sample6_9() | |
{ | |
widget6_9<policy6_9> hoge; | |
hoge.do_something(); | |
hoge.abc(); | |
hoge.first(); | |
widget6_9<fakePolicy> fuga; | |
fuga.hoge(); | |
} | |
// declare start for 6-1 | |
void sample2_20() | |
{ | |
vector<int> v; | |
v.push_back(1); | |
v.push_back(2); | |
v.push_back(3); | |
copy(begin(v), end(v) - 1, ostream_iterator<int>(std::cout, ",")); | |
cout << v.back() << endl; | |
list<double> dl; | |
dl.push_back(0.1); | |
dl.push_back(0.2); | |
dl.push_back(0.3); | |
//copy(begin(dl), end(dl), ostream_iterator<double>(std::cout, ",")); | |
} | |
void sample_boost() | |
{ | |
std::cout << boost::format("%s¥n") % "hello world"; | |
boost::shared_ptr<int> pInt(new int); | |
} | |
int main() | |
{ | |
cout << "C++ Template Samples." << endl; | |
//sample5_1(); | |
//sample5_3(); | |
//sample5_33(); | |
//sample6_1(); | |
//sample6_8(); | |
//sample6_9(); | |
//sample2_20(); | |
sample_boost(); | |
//cout << hex; | |
//cout << 12334; | |
cout << "Finished" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment