#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;
}
}