C++11 First Glimpse: ensuring stability and compatibility

Source: Internet
Author: User


1.1 Start support for macros _STDC_ and _FUNC_

Have to spit groove, g++ long support these two macros, the first macro _stdc_ also have a few derivative macros, used to control the version; _func_ use the return function name in the function definition body, not in the formal parameter list, for the reason you know, the function has not yet been declared complete.

Incidentally, the other macros supported by standard C:

__line__ inserting the current source code line number in the source code
__file__ inserting the current source code file name in the source code
__DATE__ Insert the current compilation date in the source code (note the difference from the current system date)
__TIME__ the current compilation time in the source code (note the difference from the current system time)
__STDC__ This identifier is assigned a value of 1 when the program is required to strictly follow the ansic standard.
__cplusplus when compiling with the C + + compiler, the identifier __cplusplus is defined


1.2_pragma operator overrides #pragma macro definition

Syntax: _pragma (character constant)

Benefit: The operator can participate in other macro definitions and operator operations

Instance:

#Pragma once into: _pragma ("once");

1.3 Supports long long int

It's an afterthought, and it's been used.

C Output format: Long long int:%lld,unsigned long long int:%llu

1.4 Compile-time assertion: Static_assert ()

Run-time assertion function: assert (logical expression), an expression called abort () interrupts execution when false

Static_assert (logical expression, warning message character constant);

Note: the logical expression must be in the compilation period can be asserted true and FALSE!!!

Keep in mind that Static_asset is executed at compile time and cannot be used to detect runtime values, to the parameters of the function below.

void Divide (int a, int b)  {       static_assert (b==0, "Bad arguments.....leading to division by Zero");      Sorry mate! The above check is not possible via static_assert...use some other means  }  
Static_assert This declaration is useful for debugging templates, and the compiler executes this constant expression parameter quickly (not dependent on template parameters). Otherwise, the compiler executes the arguments of this constant expression when the template is instantiated.

1.5 Introducing the Noexcept modifier and the noexcept operator

Syntactically speaking, the noexcept modifier has two forms, one of which is simply to add the Noexcept keyword after the function declaration. Like what:
void Excpt_func () noexcept;

The other one can accept a constant expression as a parameter, as follows:
void Excpt_func () noexcept (constant expression);

The result of a constant expression is converted to a value of type bool. A value of TRUE indicates that the function does not throw an exception, and conversely, it is possible to throw an exception. Here, noexcept with no constant expression is equivalent to declaring noexcept (true), which means that no exception is thrown.

1.6 Support for fast initialization of member variables

#include <string>using namespace Std;class mem {public:    mem (int i): M (i) {}private:    int m;}; Class Group {public:    Group () {}                   //There is no need to initialize data, mem, name member    Group (int a): data (a) {}    //Here does not need to initialize mem, Name Member    Group (mem m): Mem (m) {}    //This does not need to initialize data, name Member    Group (int A, mem m, string N): Data (a), Mem (m), name (n) {}private:    int data = 1;    Mem mem{0};    String name{"Group"};};/ /Compile Options: g++ 2-7-4.cpp-std=c++11-c

(1) Support is initialized at class declaration, its precedence is lower than the constructor initialization list, because the latter will happen later!!

(2) There are two ways of initializing: = and {}

(3) Static constant members and very static members are initialized in the same way: the former can be initialized inside and outside, and the latter may only be initialized out of the class declaration

1.7 Non-static members support sizeof ()

This does not change much, adding sizeof () support for non-static members of the object

#include <iostream> using namespace std;   struct people {public  :      int hand;      static people * all;  };   int main () {      people p;      cout << sizeof (P.hand) << Endl;         C++98 through, c++11 through      cout << sizeof (people::all) << Endl;    C++98 through, c++11 through      cout << sizeof (People::hand) << Endl;   Error in c++98, c++11 through  }  

1.8 Extended friend friend Syntax

See this, just want to say, C + + since then more than a Kit Kat Kinky technology!!
(1) Syntax declaration Friend class, you can not add Class

Class Poly;  typedef Poly P;   Class Lilei {      friend class Poly;  C++98 through, c++11 through  };   Class Jim {      friend Poly;        c++98 failure, c++11 through  };   Class Hanmeimei {      friend P;           c++98 failure, c++11 through  };  
(2) can declare friend class for class template!!

Class P;   Template <typename T> class people {      friend T;  };   People<p> PP;   Type P Here is the people type of friend  people<int> Pi;//for the int type template parameter, the friend declaration is ignored  
For people this template class, p is a friend class for people<p> when using class P as the template parameter. When using the built-in type int as the template parameter, the,people<int> is instantiated as a normal type without a friend definition. This allows us to determine whether a template class has friends and who is the friend of the template class when the template is instantiated.

1.9 Multi-state overload control: Final/override Control

(1) Final reject Reload

(2) Override label overload

struct object{    virtual void fun () = 0;}; struct Base:public Object {    void fun () final;   Declared as Final};struct derived:public Base {    void fun ();     Unable to compile};//compilation option: g++-c-std=c++11 2-10-2.cpp

struct Base {    virtual void Turing () = 0;    virtual void Dijkstra () = 0;    virtual void Vneumann (int g) = 0;    virtual void Dknuth () const;    void Print ();}; struct Derivedmid:public Base {    //void Vneumann (double g);    The interface was isolated, and had thought of one more version of the Vneumann function};struct derivedtop:public derivedmid {    void Turing () override;    void Dikjstra () override;           cannot be compiled, misspelled, not overloaded    void Vneumann (double g) override;   cannot be compiled, parameters are inconsistent, not overloaded    void Dknuth () override;             cannot be compiled, constant inconsistent, not overloaded    void Print () override;              Unable to compile, non-virtual function overload};//compilation option: g++-c-std=c++11 2-10-3.cpp

1.10 Template Functions support default parameters

The template class is also supported.

For multi-parameter template classes and template functions, the requirements are not the same!!!

The default formal parameter writing order of the template class must be from right to left, in order to facilitate the type deduction of the template class when it is present

Template function arbitrary

Template<typename T1, TypeName T2 = Int> class DefClass1;  Template<typename T1 = int, typename t2> class DefClass2;   cannot be compiled by   template<typename T, int i = 0> class DefClass3;  Template<int i = 0, TypeName t> class DefClass4;            Failed to compile   template<typename T1 = int, typename t2> void DefFunc1 (T1 A, T2 b);  Template<int i = 0, typename t> void DefFunc2 (T a);  Compilation options: g++-c-std=c++11 2-11-2.cpp  
The parameter derivation rules for function templates are also not complex. Simply put, if you can derive a type from a function argument, the default template parameter will not be used, whereas the default template parameter may be used.

Template <class T, class U = double> void f (t t = 0, u u = 0);   void G () {      f (1, ' C ');      F<int,char> (1, ' C ')      f (1);           F<int,double> (1,0), using the default template parameter Double      f ();            Error: T cannot be deduced      f<int> ();       F<int,double> (0,0), using the default template parameter double      f<int,char> ();  F<int,char> (0,0)  }  



C++11 First Glimpse: ensuring stability and compatibility

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.