Range Based For


Range Based For is new in c++11

void RangeBasedFor()
{
	nullptr_t pN = nullptr;
	void *pV = reinterpret_cast(1);
	int *pI = reinterpret_cast(2);
	void *arr[] = { pV, pN, pI };
	std::cout << "enumerating void *arr[] with Range Based For syntax" << std::endl;
	//other options: auto i, auto&& i
	for (const auto &i : arr) //auto or int, arr can be replaced directly by {2, 3}
		std::cout << i << std::endl;

	std::vector arr2 = { 3, 4 };
	std::cout << "enumerating vector with Range Based For syntax" << std::endl;
	for (const auto &i : arr2) //works for both int[] and vector
		std::cout << i << std::endl;
	std::cout << "enumerating vector with regular for loop" << std::endl;
	for(std::vector::iterator i = arr2.begin(); i != arr2.end(); ++i) //only for vector
		std::cout << *i << std::endl;
}

 

Output:
enumerating void *arr[] with Range Based For syntax
0000000000000001
0000000000000000
0000000000000002
enumerating vector with Range Based For syntax
3
4
enumerating vector with regular for loop
3
4

 

Function Hiding


If there is a functions in base class and one in subclass that has the same name,
the one in subclass always hides the one in base class, regardless of the parameter list and virtual function or not.
Access base class function through sub class object with a different parameter is a compiling error.
Consider this code snippet:

struct CBase
{
	void funcNonVirtual(int i)
	{
		printf("CBase::funcNonVirtual\n");
	}
};
struct CSub: CBase
{
	void funcNonVirtual(char * string)
	{
		printf("CSub::funcNonVirtual\n");
	}
};
void TestHide()
{
	CSub Sub;
	Sub.funcNonVirtual(2); //comiling error
}

Variadic templates


A function can take any number any type of arguments in in cpp11

template <class T>
void VariadicPrint(T tail)
{
	cout << tail << endl;
}

template <class T, class ...Args>
void VariadicPrint(T head, Args... rest)
{
	cout << head << " ";
	VariadicPrint(rest...);
}

void TestVariadic()
{
	VariadicPrint(1, "a"); //output: 1 a
}

VariadicPrint(1, “a”) outputs “1 a”

decltype in lambda


decltype is to calculate type from object expression
Demonstrated by the lambda used to initialize std::set
decltype is required since that’s the only way to access the type of the lambda when the lambda is embedded.

lambda is no more than syntactic shortcut to functor.

void TestLambdaInSet()
{
	bool bGreat = true;
	auto cmp = [=, &bGreat](int a, int b)
	{
		if (bGreat)
			return a > b;
		else
			return a < b;
	};
	std::set myset(cmp);
	myset.insert(1); myset.insert(2);
	printf("first item should be greatest since bGreat = true. got %d", *myset.begin());
	//output is 2 as expected
	bGreat = false;
	myset.clear();
	myset.insert(1); myset.insert(2);
	printf("first item should be smallest since bGreat = false. got %d", *myset.begin());
	//output is 1 as expected
}

void main()
{
	TestLambdaInSet();
}