第一部分 语言
P1 演进、环境与资源
#include <iostream>
using namespace std;
int main ( )
{
cout << __cplusplus << endl;
return 0 ;
}
P2 Variadic Templates
#include <iostream>
using namespace std;
void print ( ) {
}
template < typename T, typename . . . Types>
void print ( const T & arg1, const Types & . . . args)
{
cout << "Size of args: " << sizeof . . . ( args) << " | " << arg1 << endl;
print ( args. . . ) ;
}
template < typename . . . Types>
void print ( const Types & . . . args)
{
cout << "void print(const Types &... args)" << endl;
}
int main ( )
{
print ( 1 , 2.3 , '4' , "567" ) ;
return 0 ;
}
P3 Spaces in Template Expressions,nullptr and std::nullptr_t,automatic type deduction
#include <iostream>
#include <vector>
using namespace std;
void f ( int )
{
cout << "void f(int);" << endl;
}
void f ( void * )
{
cout << "void f(void*);" << endl;
}
int g ( )
{
return 10 ;
}
int main ( )
{
vector< vector< int > > a;
vector< vector< int >> b;
f ( 0 ) ;
f ( nullptr ) ;
auto i = 10 ;
auto j = g ( ) ;
auto k = [ ] ( int x) - > int {
x * = 10 ;
return x + 1 ;
} ;
cout << k ( 10 ) << endl;
return 0 ;
}
P4 Uniform Initialization
#include <iostream>
#include <vector>
#include <complex>
#include <string>
using namespace std;
int main ( )
{
int values[ ] {
1 , 2 , 3 } ;
int a = 4 , b = 5 , d{
6 } ;
vector< int > v{
2 , 3 , a, b, d} ;
vector< string> ss{
"123" , "456" , "789" } ;
complex< double > c{
4.0 , 3.0 } ;
return 0 ;
}
P5、P6 initializer_list
#include <iostream>
#include <vector>
#include <array>
#include <complex>
#include <string>
using namespace std;
template < typename T>
void print ( initializer_list< T> vals)
{
cout << vals. size ( ) << " : " ;
for ( auto p = vals. begin ( ) ; p != vals. end ( ) ; p++ )
{
cout << * p << " " ;
}
cout << endl;
}
void print ( initializer_list< int > vals)
{
cout << vals. size ( ) << " : " ;
for ( auto p = vals. begin ( ) ; p != vals. end ( ) ; p++ )
{
cout << * p << " " ;
}
cout << endl;
}
void print ( initializer_list< int > vals, initializer_list< double > ds)
{
cout << vals. size ( ) + ds. size ( ) << " : " ;
for ( auto p = vals. begin ( ) ; p != vals. end ( ) ; p++ )
{
cout << * p << " " ;
}
for ( auto p = ds. begin ( ) ; p != ds. end ( ) ; p++ )
{
cout << * p << " " ;