C++ 가변 길이 매개변수 템플릿(Variadic Templates)

5467 단어
/* 
 * File:   main.cpp
 * Author: Vicky.H
 * Email:  [email protected]
 */
#include <iostream>

template<int Index, typename... Elements>
struct Tuple_Impl;

template<int Index>
struct Tuple_Impl<Index> {
};

template<int Index, typename Head, typename... Tail>
struct Tuple_Impl<Index, Head, Tail...> : public Tuple_Impl<Index + 1, Tail...> {
};


// extend template template

template<typename T>
struct eval {
};

// specialization
template<
    template<typename, typename...> class TT,
    typename T1,
    typename... Rest
>
struct eval<TT<T1, Rest... >>
{

    void operator=(int) {
    };
};

template<typename T1> struct A;
template<typename T1, typename T2> struct B;
template<int N> struct C;
template<typename T1, int N> struct D;
template<typename T1, typename T2, int N = 17 > struct E;


/*
 *  :variadic templates   -std=c++0x   -std=gnu++0x  
 */
int main(void) {
    eval < A<int >> ea;
    eval < B<int, float >> eb;
    eval < C < 17 >> ec;
    eval < D<int, 17 >> ed;
    eval < E<int, float >> ee;

    return 0;
}
/* 
 * File:   main.cpp
 * Author: Vicky.H
 * Email:  [email protected]
 */
#include <iostream>

/** */
template<typename ... Args>
std::size_t sizeof_args(Args ... args){
    return sizeof...(args);
}

template<typename T>
inline T add() {
    std::cout << "DONE2" << std::endl;
    return 0;
}

template<typename T, typename... Args >
inline T add(T && t, Args && ... args) {
    std::cout << "sizeof_args(args...) : " << sizeof_args(args...) << std::endl;
    return t + add<T> (args...);
}

/*
 * 
 */
int main(void) {
    std::cout << add(1.0,2.0,3.0,4.0,5.0) << std::endl;
    std::cout << "---------------------------" << std::endl;
    std::cout << add(10,20,30,40,50,60,70,80,90) << std::endl;
    
    return 0;
}
/* 
 * File:   main.cpp
 * Author: Vicky.H
 * Email:  [email protected]
 */
#include <iostream>

int add(int i, int j) {
    return i + j;
}

int doadd(int (*f)(int, int), int i, int j) {
    f(i, j);
}

template <typename T>
T plus() {
    return 0;
}

//----

template <typename T>
void func01(T& o1, T& o2) {
    std::cout << "func01 begin o1 = " << o1 << " o2 = " << o2 << std::endl;
    o1 += o2;
    std::cout << "func01 end   o1 = " << o1 << " o2 = " << o2 << std::endl;
}

template <typename T>
void func02(T& o1, T& o2) {
    std::cout << "func02 begin o1 = " << o1 << " o2 = " << o2 << std::endl;
    o1 -= o2;
    std::cout << "func02 end   o1 = " << o1 << " o2 = " << o2 << std::endl;
}

template <typename T>
void dofunc(void (*func01)(T&, T&), void (*func02)(T&, T&), T& o1, T& o2) {
    func01(o1, o2);
    func02(o1, o2);
}

//----
template <typename T>
void dofunclist() {}

template <typename T,typename ...F>
void dofunclist(F(* ...funcs)(T&,T&)) {
}

/*
 * 
 */
int main(void) {

    std::cout << "dofunc(&add,10,20) = " << doadd(&add, 10, 20) << std::endl;

    std::cout << "---------------------------" << std::endl;

    int i = 10;
    int j = 10;
    dofunc(func01, func02, i, j);

    return 0;
}
/* 
 * File:   main.cpp
 * Author: Vicky.H
 * Email:  [email protected]
 */
#include <iostream>
#include <string.h>

class GameObj {
public:
    virtual void attack() = 0;

    virtual ~GameObj() { }
};

class Player : public GameObj {
public:

    Player(const char* _name) {
        strcpy(name, _name);
    }

    void attack() {
        std::cout << "PLAYER[" << name << "] attack" << std::endl;
    }
private:
    char name[10];
};

class Monster : public GameObj {
public:

    Monster(const char* _name) {
        strcpy(name, _name);
    }

    void attack() {
        std::cout << "MONSTER[" << name << "] attack" << std::endl;
    }
private:
    char name[10];
};

inline void attack() {
    std::cout << "OVER" << std::endl;
}

template <typename T, typename ... Args>
inline void attack(T&& o, Args&& ... args) {
    static int i = 0;
    std::cout << &i /**i !*/ << "\t" << i++ << "\t";
    o.attack();
    attack(args...);
}

inline void attack2() {
    static int i = 0;
    std::cout << i++ << std::endl;
}


/*
 * 
 */
int main(void) {

    Player p1("p1");
    Player p2("p2");
    Player p3("p3");
    Player p4("p4");
    Player p5("p5");

    Monster m1("m1");
    Monster m2("m2");
    Monster m3("m3");

    attack(p1, m1, p2, m2, p3, m3, p4, p5);
        
    std::cout << "---------------------------" << std::endl;
    
    for (int i = 0; i < 10; i++) {
        attack2(); //  !!!!
    }

    return 0;
}

좋은 웹페이지 즐겨찾기