-
Notifications
You must be signed in to change notification settings - Fork 78
C++11 Programming
bellbind edited this page Oct 26, 2013
·
8 revisions
Almost all declared statement can use only auto name = ....
-
auto i = 0;: The type ofiissigned int. Warning occurred when comparing tounsignedvariables-
auto i = 0u;:iasunsigned int
-
int outer1 = ...;
int outer2 = ...;
// capture outer1 as value and outer2 as reference
auto func = [outer, &outer2](int arg1, void* arg2) -> void {
};
// type of func is "void (*func)(int, void*)"It can use this type of function declaration at named functions. e.g.
auto func(int, void*) -> void;
auto func(int arg1, void* arg2) -> void {
}
using func_t = void (*)(int, void*);
int main()
{
func_t f = func;
f(0, nullptr);
return 0;
}But this type notation cannot be used at statements of variable and typedef.
(using is available instead of function typedef)
-
const_cast<t>(v): removeconstfromconst t v -
dynamic_cast<t>(v): object down cast (runtime cast) -
static_cast<t>(v): C down cast (compile time cast). wheno = static_cast<t>(v),t o = vis valid in C.- from
void*to other pointers, usestatic_cast
- from
-
reinterpret_cast<t>(v): force cast as a memory fragment
For non-pointer arguments in inline function,
use type const T& instead of T
to avoid call copy constructor.
e.g. from:
T obj1
char* obj2
//do something with obj1 and obj2
...as
inline void func(const T& arg1, const char* arg2) {...}
T obj1
char* obj2
//do something with obj1 and obj2
func(obj1, obj2);