C/C++ 可变参数模版(variadic templates)

#include <iostream>
using namespace std;
template <typename T, typename ... ARGS>
unique_ptr<T> factory(const ARGS&... args)
{
    return unique_ptr<T>(new T { args... });
}
struct A { };
struct B { int a; int b; };
struct C { int a; int b; string c; };
struct D
{
public:
    D(int a) {}
};

int main()
{
    auto a = factory<A>();
    auto b = factory<B>(10, 20);
    auto c = factory<C>(30, 40, "Hello");
    auto d = factory<D>(10);

    cout << c->c << endl;

}

Hello

#include <iostream>
#include <string>

using namespace std;

struct A { };
struct B { int a; int b; };
struct C { int a; int b; string c; };
struct D
{
public:
    D(int a) {}
};

struct E
{
    int& e;
    E(int& e) : e(e) { e++; }
};
//
template <typename T, typename ... ARGS>
unique_ptr<T> factory(ARGS&&... args)
{
    return unique_ptr<T>(new T { std::forward<ARGS>(args)... });
}
/
//template <typename T, typename ... ARGS>
//unique_ptr<T> factory(const ARGS&... args)
//{
//    return unique_ptr<T>(new T { args... });
//}
///
int main()
{
    auto a = factory<A>();
    auto b = factory<B>(10, 20);
    auto c = factory<C>(30, 40, "Hello");
    auto d = factory<D>(10);

    int x = 2;
    auto e = factory<E>(x);
    cout << x << endl;
}

3

#include <iostream>
 
using namespace std;
 
struct X
{
    X() { cout << "ctor" << endl; }
    X(const X&) { cout << "copy ctor" << endl; }
    X(X&&) { cout << "move ctor" << endl; }
};
 
struct Wrapper
{
    X w;
    Wrapper(const X& w) : w(w) { }
};
 
int main()
{
    Wrapper w1(X { });
    cout << "***" << endl;
    X y;
    Wrapper w2(y);
}

ctor
copy ctor
***
ctor
copy ctor

#include <iostream>
 
using namespace std;
 
struct X
{
    X() { cout << "ctor" << endl; }
    X(const X&) { cout << "copy ctor" << endl; }
    X(X&&) { cout << "move ctor" << endl; }
};
 
struct Wrapper
{
    X w;
    Wrapper(const X& w) : w(w) { }
    Wrapper(X&& w): w(std::move(w)) { } //Adding a overloaded constructor
};
 
int main()
{
    Wrapper w1(X { });
    cout << "***" << endl;
    X y;
    Wrapper w2(y);
}

ctor
move ctor
***
ctor
copy ctor

#include <iostream>
using namespace std;
struct X
{
    X() { cout << "ctor" << endl; }
    X(const X&) { cout << "copy ctor" << endl; }
    X(X&&) { cout << "move ctor" << endl; }
};
 
struct Wrapper
{
    X w;
    template <typename Q>
    Wrapper(Q&& w) : w(std::forward<Q>(w)) { }
};
 
int main()
{
    Wrapper w1(X { });
    cout << "***" << endl;
    X y;
    Wrapper w2(y);
}
#include<bits/stdc++.h>
template <typename ... Tail> class Tuple;

template<> class Tuple<>{};

template <typename Value,typename ... Tail>
class Tuple<Value,Tail ...>:Tuple<Tail ...>{
    Value Val;
public:
    Tuple(){}
    Tuple(Value value,Tail ... tail):Val(value),Tuple<Tail ...>(tail ...){}
    Value value(){return Val;}
    Tuple<Tail ...> next(){return *this;}
};

int main(){
    Tuple<char,double,std::string> tuple('1',1.5,"Hello World");
    std::cout << tuple.value() << std::endl;
    std::cout << tuple.next().value() << std::endl;
    std::cout << tuple.next().next().value() << std::endl;
    return 0;
}

1
1.5
Hello World

#include <functional>
#include <iostream>
using namespace std;
template<typename T>

//普通的模版函数

bool pair_comparer(T a, T b) {
    return a == b;
}

//这里typename... 的参数args  代表了上面的普通的模版函数

template<typename T, typename... Args>
bool pair_comparer(T a, T b, Args... args) {
    return a == b  && pair_comparer(args...);
}

int main()
{
//args 的参数可以增加

 if (pair_comparer(1, 1,2,2,3,3,3,3))
    {
        cout << "in" << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值