// 11111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <string> template<typename T>
class AccumulationTraits; template<>
class AccumulationTraits<char> {
public:
typedef int Acct;
static Acct zero() {
return 0;
}
}; template<>
class AccumulationTraits<int> {
public:
typedef long Acct;
static Acct zero() {
return 0;
}
}; template<>
class AccumulationTraits<float> {
public:
typedef double Acct;
static Acct zero() {
return 0;
}
};
/*
template<typename T,typename AT = AccumulationTraits<T> >
class Accum {
public:
static typename AT::Acct accum(T const* beg, T const* end) {
typename AT::Acct total = AT::zero();
while (beg != end)
{
total += *beg;
++beg;
}
return total;
}
}; template<typename T>
typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
{
std::cout << "T" << std::endl;
return Accum<T>::accum(beg,end);
} template<typename Traits, typename T>
typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
{
std::cout << "T Traits" << std::endl;
return Accum<T,Traits>::accum(beg, end);
}
*/
template<typename T1, typename T2>
class SumPolicy {
public:
static void accumulate(T1& total, T2 const& value) {
total += value;
}
}; template<typename T,
template<typename,typename>class Policy = SumPolicy,
typename Traits = AccumulationTraits<T> >
class Accum {
public:
typedef typename Traits::Acct Acct;
static Acct accum(T const* beg,T const* end) {
Acct total = Traits::zero();
while (beg != end)
{
Policy<Acct, T>::accumulate(total, *beg);
++beg;
}
return total;
} }; int main()
{
int num[] = { 1,2,3,4,5 };
std::cout << Accum<int>::accum(&num[0],&num[5]) << std::endl; return 0;
}
// 11111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <string> template<typename T>
class AccumulationTraits; template<>
class AccumulationTraits<char> {
public:
typedef int Acct;
static Acct zero() {
return 0;
}
}; template<>
class AccumulationTraits<int> {
public:
typedef long Acct;
static Acct zero() {
return 0;
}
}; template<>
class AccumulationTraits<float> {
public:
typedef double Acct;
static Acct zero() {
return 0;
}
};
/*
template<typename T,typename AT = AccumulationTraits<T> >
class Accum {
public:
static typename AT::Acct accum(T const* beg, T const* end) {
typename AT::Acct total = AT::zero();
while (beg != end)
{
total += *beg;
++beg;
}
return total;
}
}; template<typename T>
typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
{
std::cout << "T" << std::endl;
return Accum<T>::accum(beg,end);
} template<typename Traits, typename T>
typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
{
std::cout << "T Traits" << std::endl;
return Accum<T,Traits>::accum(beg, end);
}
*/
template<bool b = true>
class SumPolicy {
public:
template<typename T1, typename T2>
static void accumulate(T1& total, T2 const& value) {
total += value;
}
}; template<>
class SumPolicy<false> {
public:
template<typename T1, typename T2>
static void accumulate(T1& total, T2 const& value) {
total = total+value;
}
}; template <typename T,
typename Policy = SumPolicy<>,
typename Traits = AccumulationTraits<T> >
class Accum {
public:
typedef typename Traits::Acct Acct;
static Acct accum(T const* beg, T const* end) {
Acct total = Traits::zero();
while (beg != end) {
Policy::accumulate(total, *beg);
++beg;
}
return total;
}
}; int main()
{
int num[] = { 1,2,3,4,5 };
std::cout << Accum<int>::accum(&num[0], &num[5]) << std::endl;
std::cout << Accum<int,SumPolicy<false>>::accum(&num[0],&num[5]) << std::endl;
std::cout << Accum<int, SumPolicy<true>>::accum(&num[0], &num[5]) << std::endl; return 0;
}
// 111111.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h"
#include <iostream> template<typename T>
class IsFundaT {
public:
enum{Yes = 0,No=1};
}; #define MK_FUNDA_TYPE(T) \
template<> class IsFundaT<T> { \
public: \
enum{Yes =1,No=0}; \
};
MK_FUNDA_TYPE(void)
MK_FUNDA_TYPE(bool)
MK_FUNDA_TYPE(signed char)
MK_FUNDA_TYPE(unsigned char)
MK_FUNDA_TYPE(wchar_t) MK_FUNDA_TYPE(signed short)
MK_FUNDA_TYPE(unsigned short)
MK_FUNDA_TYPE(signed int)
MK_FUNDA_TYPE(unsigned int)
MK_FUNDA_TYPE(signed long)
MK_FUNDA_TYPE(unsigned long)
#if LONGLONG_EXISTS
MK_FUNDA_TYPE(signed long long)
MK_FUNDA_TYPE(unsigned long long)
#endif // LONGLONG_EXISTS MK_FUNDA_TYPE(float)
MK_FUNDA_TYPE(double)
MK_FUNDA_TYPE(long double) //====================================================== template<typename T>
void test(T const& t) {
if (IsFundaT<T>::Yes) {
std::cout << "T is fundamental type" << std::endl;
}
else
{
std::cout << "T is no fundamental type" << std::endl;
}
} class MyType{ }; int main()
{
int i = 9;
auto ii = std::move(i);
test(ii);
test(7);
test(MyType()); return 0;
}