- // TestCpp0x.cpp : Defines the entry point for the console application.
- //
- //test
- #include "stdafx.h"
- #include <vector>
- #include <string>
- #include <functional>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- ////////////////////////////////////////
- // auto
- void TestAuto()
- {
- auto i = 3;
- auto d = 3.14;
- auto l = 1000L;
- auto f = 3.14f;
- //auto nError; //Error: error C3531: 'nError': a symbol whose type contains 'auto' must have an initializer
- auto p = &i;
- auto pp = &p;
- auto q = *pp;
- auto& iTemp = i;
- iTemp = 4;
- auto pNewI = new int(3);
- auto pNewIntArry = new int[3];
- delete pNewI;
- delete[] pNewIntArry;
- const auto pi = 3.14;
- const auto const pi2 = &d;
- auto dd = 6.28;
- //pi2 = ⅆ
- *pi2 = 6.28;
- const double* const pi3 = &d;
- //pi3 = ⅆ
- //*pi3 = 6.28;
- volatile auto IsFinished = false;
- //auto myArray[10];
- vector<int> testVector;
- for(int i=0; i<=10; ++i)
- testVector.push_back(i);
- auto vSize = testVector.size();
- for(decltype(vSize) j = 0; j<vSize; ++j)
- {
- printf("%d ", testVector[j]);
- }
- // Or
- for(decltype(testVector.size()) j = 0; j<vSize; ++j)
- {
- printf("%d ", testVector[j]);
- }
- printf("\n");
- vector<string> strings;
- strings.push_back("AAA");
- strings.push_back("BBB");
- strings.push_back("CCC");
- for(vector<string>::const_iterator iter = strings.begin(); iter != strings.end(); ++iter)
- {
- printf("%s \n", (*iter).c_str());
- }
- for(auto iter = strings.begin(); iter != strings.end(); ++iter)
- {
- printf("%s \n", (*iter).c_str());
- }
- // To assign Lambdas to a variable
- // To specify Trailing Return Types
- }
- void TestDeclType()
- {
- int i = 3;
- decltype(i) j = 4;
- decltype(1/0) t = 5;
- decltype(sizeof(3)) s = 6;
- }
- void func(int* p)
- {
- printf("func(int* p)\n");
- }
- void func(int p)
- {
- printf("func(int p)\n");
- }
- void TestNullPtr()
- {
- func(nullptr);
- func(NULL);
- }
- void TestStaticAssert()
- {
- //static_assert(1==2, "1 is not equal to 2"); //error C2338: 1 is not equal to 2
- static_assert(sizeof(void *) == 4, "This code should only be compiled as 32-bit.");
- //_STATIC_ASSERT
- }
- void TestLambda()
- {
- auto lam1 = [](){};
- auto squareLam = [](int n) { return n*n; };
- int result = squareLam(2);
- int result2 = squareLam(squareLam(2));
- double pi = []{ return 3.14159; }();
- int result3 = [](int n) { return n*n; }(3);
- int nMax = [](int n1, int n2) {
- return (n1>n2) ? (n1) : (n2);
- } (56, 11);
- int nMin = [](int n1, int n2) {
- return (n1<n2) ? (n1) : (n2);
- } (984, 658);
- auto squareLam2 = [](int n) ->int { return n*n; };
- int nAbs = [] (int n1) -> int //It's necessary
- {
- if(n1<0)
- return -n1;
- else
- return n1;
- }(-109);
- /*
- [] - Capture nothing.
- [=] - Capture everything by value.
- [&] - Capture everything by reference.
- [var] - Capture var by value; nothing else, in either mode, is captured.
- [&var] - Capture var by reference; nothing else, in either mode, is captured.
- */
- int a=10, b=20, c=30;
- [a](void) mutable // Capturing ONLY 'a' by value
- {
- // cannot modify
- a++; // error C3491: 'a': a by-value capture
- // cannot be modified in a non-mutable lambda
- printf("a = %d", a);//11
- }();
- printf("a = %d", a);//10
- int nSum =0;
- [=, &nSum]
- {
- nSum = a+b+c;
- }();
- tr1::function<bool (int)> IsEven = [](int n){ return n%2 == 0; } ;
- bool bEven = IsEven(41);
- vector<int> myVector;
- for(int i=0; i<10; ++i)
- myVector.push_back(i);
- printf("\n");
- for_each(myVector.begin(), myVector.end(), [](int n){ printf("%d ", n);});
- int sum = 0;
- for_each(myVector.begin(), myVector.end(), [&sum](int n){ sum += n; } );
- }
- auto GetPi() -> double
- {
- return 3.14;
- }
- void TestTrailingType()
- {
- double pi = GetPi();
- }
- //Begin
- class MyMemoryBlock
- {
- public:
- MyMemoryBlock(int size = 10)
- :m_nSize(size)
- {
- m_pMemory = new unsigned char[size];
- }
- ~MyMemoryBlock()
- {
- if(m_pMemory)
- delete[] m_pMemory;
- }
- MyMemoryBlock(const MyMemoryBlock& another)
- {
- m_nSize = another.m_nSize;
- m_pMemory = new unsigned char[m_nSize];
- memcpy(m_pMemory, another.m_pMemory, m_nSize);
- }
- /*
- MyMemoryBlock(MyMemoryBlock&& another)
- {
- m_nSize = another.m_nSize;
- m_pMemory = another.m_pMemory;
- another.m_nSize = 0;
- another.m_pMemory = NULL;
- }
- */
- private:
- unsigned char* m_pMemory;
- int m_nSize;
- };
- MyMemoryBlock getMyMemoryBlock()
- {
- MyMemoryBlock tempM;
- return tempM;
- }
- void TestRValueReference()
- {
- MyMemoryBlock m(getMyMemoryBlock());
- }
- //End
- // The compiler treats a named rvalue reference as an lvalue and an unnamed rvalue reference as an rvalue.
- // Begin
- // A class that contains a memory resource.
- class MemoryBlock
- {
- // TODO: Add resources for the class here.
- };
- void g(const MemoryBlock&)
- {
- cout << "In g(const MemoryBlock&)." << endl;
- }
- void g(MemoryBlock&&)
- {
- cout << "In g(MemoryBlock&&)." << endl;
- }
- MemoryBlock&& f(MemoryBlock&& block)
- {
- g(block);
- // return block; // It will report error when compiling
- //return static_cast<MemoryBlock&&>(block); // It works
- return std::move(block);
- }
- MemoryBlock ff()
- {
- MemoryBlock mb;
- return mb;
- }
- int gg(const MemoryBlock& block)
- {
- return 1;
- }
- void TestRValueReference2()
- {
- g(f(MemoryBlock()));
- int i = gg(ff());
- }
- // End
- void TestDoubleBracket()
- {
- vector<vector<int>> myVector;
- }
- template<class T>
- struct vector_tth
- {
- typedef std::vector<T, allocator<T>> R;
- };
- // Not implemented in VC10
- //template<class T>
- //using vectorWithAllocator = std::vector<T>;
- void TestTemplateUsing()
- {
- vector_tth<int>::R myIntVector;
- }
- enum Priority : unsigned char // unsigned char
- {
- VeryLow = 0,
- Low,
- Medium,
- High,
- VeryHigh
- };
- void TestStrongTypeEnum()
- {
- Priority priorirty = VeryHigh;
- }
- struct A {
- int _0;
- };
- A f()
- {
- A a;
- return a;
- }
- typedef A (*FuncDef)();
- A g(FuncDef funcDef)
- {
- return funcDef();
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- //A a0(A());
- //decltype(&a0) b = NULL;
- //int i=3;
- //A (*a0_p0)();
- //a0_p0 = f;
- //a0 a;
- //a = g;
- //a(a0_p0); // CANNOT LINK!!!
- //A a1(A::A());
- //a1._0;
- int a =0;
- scanf("%d", &a);
- TestAuto();
- //TestDeclType();
- //TestNullPtr();
- //TestStaticAssert();
- //TestLambda();
- /*TestTrailingType();
- TestRValueReference();
- TestRValueReference2();*/
- return 0;
- }