+ Reply to Thread
Results 1 to 2 of 2

Thread: What is 'auto' keyword in new C++ standard?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    48

    What is 'auto' keyword in new C++ standard?

    What is 'auto' keyword in new C++ standard?

  2. #2
    The 'auto' keyword directs the compiler to deduce the type of a declared variable from its initialization expression. The previous meaning of the auto keyword as the specifier for the automatic storage-class is now an error.

    Code:
    void UseAutoKeyword()
    {
    	//auto keyword can be used like a template...
    	//a. you do not need to specify data type.
    	//b. you must have to initialize the auto variable when it is declared!
    	auto nInteger = 42 ; //this is int!
    
    	char szMessage[100] ;
    	sprintf_s(szMessage, "Value = %d", nInteger) ;
    
    	auto szText = "I am a good boy!!!" ; //this is string!
    	sprintf_s(szMessage, "Value = %s", szText) ;
    
    	std::vector<int> vInts ;
    
    	for(int nIndex=0; nIndex<5; nIndex++)
    	{
    		vInts.push_back(nIndex) ;
    	}
    
    	auto itr = vInts.begin() ;//this is real magic behind using 'auto' keyword!
    	for(; itr != vInts.end() ; itr++)
    	{
    		sprintf_s(szMessage, "Value = %d", *itr) ;
    	}
    }
    Note:
    a. For more information : http://msdn.microsoft.com/en-us/library/dd293667.aspx
    b. Above example is compiled using VS 2010.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts