2 06 Современный C (ч.2)
2 06 Современный C (ч.2)
ОРИЕНТИРОВАННОЕ
ПРОГРАММИРОВАНИЕ
Лекция № 2 / 6
08.10.2019 г.
NEW & DELETE
1. Key-word new, delete.
// Global functions
void* operator new(size_t);
void* operator new[](size_t);
void operator delete(void *);
void operator delete[](void *);
PLACEMENT NEW
class Arena {
public:
Arena(int x) {};
~Arena() {};
// ...
};
struct unused;
template<typename T1 = unused, typename T2 = unused,
/*up to*/ typename TN = unused> class tuple;
template<typename T1>
tuple<T1> make_tuple(const T1& t1)
{ return tuple<T1>(t1); }
• Code duplication.
#define VARIADIC_MACRO(...)
try{
// Try block.
}
catch(...){ template parameter pack
// Catch block.
} function parameter pack
• Multiple
template parameter packs are permitted for function
templates.
//???
template <typename... Types, typename T>
void test(T value);
//???
template <typename... Xs, typename... Ys>
struct Zip<TypeList<Xs...>, TypeList<Ys...>>{};
TEMPLATE PARAMETER PACK
//Error. Template parameter pack is not the last template
//parameter.
template <typename... Types, typename Last>
class LastType;
Syntactic expansion
for 2 parameters
Syntactic expansion
for 3 parameters
Syntactic expansion
for 2 parameters
//Non-template function
void print()
{
}
//Example
std::string s("world");
print(7.5, "hello", s);
CALL STACK
//Example
std::string s("world");
print(7.5, "hello", s);
CALL STACK
//template function
print<double>(7.5);
//variadic template function
print<char const*, std::string>("hello", s);
//template function
print<char const*>("hello");
//template function
print<std::string>(s);
COUNTING ARGUMENTS
template <typename... Args> struct count;
template <>
struct count<>{
static const int value = 0;
};
//fold expression
template <typename... T>
auto foldSum(T... s){
return (0 + ... + s);
}
//Example
std::string s("world");
print(7.5, "hello", s);
FOLD EXPRESSIONS C++17