#include <string>
#include <memory>
template<typename T>
void swap(T& a, T& b){
T tmp = a;
a = b;
b = tmp;
}
template<typename T>
void print(T a){
std::cout <<"print1 : " << a << std::endl;
}
template<typename T>
void print(T a, T b){
std::cout <<"print2 : " << a <<", " << b << std::endl;
}
template<typename... Args>
void prints(Args... args){
print(args...);
return;
}
class C{
public:
template<typename... Args>
static std::shared_ptr<C> create(Args&&... args){
return std::shared_ptr<C>(new C(args...));
}
private:
explicit C(const int& a, int& b){
printf("construct C(%d, %d)\n", a, b);
int tmp = a;
//a = b;
b = tmp;
}
};
int main()
{
int a = 10;
int b = 20;
prints(a); // variadic templates
prints(a,b);
printf("before swap : %d, %d\n", a, b);
swap(a,b); // == swap<int>(a,b)
printf("after swap : %d, %d\n", a, b);
std::shared_ptr<C> c1 = C::create(a, b);
printf("after construct : %d, %d\n", a, b);
return 1;
}
No comments:
Post a Comment