enable_if
#include <iostream>
template <typename T>
typename std::enable_if<std::is_integral<T>::value, bool>::type test1(T t) {
std::cout << "integral: " << t << std::endl;
}
template <typename T>
typename std::enable_if<!std::is_integral<T>::value, bool>::type test1(T t) {
std::cout << "string: " << t << std::endl;
}
int main(int argc, char const *argv[]) {
test1(1);
test1("string");
return 0;
}
构造函数
#include <iostream>
class Example {
public:
int index_;
public:
Example(int index) : index_(index) {
std::cout << "Example(int index) : " << index_ << std::endl;
}
Example(const Example &e) {
index_ = e.index_;
std::cout << "Example(const Example &e) : " << index_ << std::endl;
}
Example &operator=(const Example &e) {
index_ = e.index_;
std::cout << "Example &operator=(const Example &e) : " << index_ << std::endl;
return *this;
}
~Example() { std::cout << "~Example() : " << index_ << std::endl; };
};
int main(int argc, char const *argv[]) {
std::cout << "----------ex1----------" << std::endl;
Example ex1(1);
std::cout << "----------ex2----------" << std::endl;
Example ex2(ex1);
std::cout << "----------ex3----------" << std::endl;
Example ex3 = ex1;
std::cout << "----------ex2=ex1----------" << std::endl;
ex2 = ex1;
std::cout << "----------end----------" << std::endl;
return 0;
}
输出:
----------ex1----------
Example(int index) : 1
----------ex2----------
Example(const Example &e) : 1
----------ex3----------
Example(const Example &e) : 1
----------ex2=ex1----------
Example &operator=(const Example &e) : 1
----------end----------
~Example() : 1
~Example() : 1
~Example() : 1