官术网_书友最值得收藏!

New rules for type auto-detection from braced initializer list 

C++17 introduced new rules for auto-detection of the initializer list, which complements C++14 rules. The C++17 rule insists that the program is ill-formed if an explicit or partial specialization of std::initializer_list is declared:

#include <iostream>
using namespace std;

template <typename T1, typename T2>
class MyClass {
private:
T1 t1;
T2 t2;
public:
MyClass( T1 t1 = T1(), T2 t2 = T2() ) { }

void printSizeOfDataTypes() {
cout << "\nSize of t1 is " << sizeof ( t1 ) << " bytes." << endl;
cout << "\nSize of t2 is " << sizeof ( t2 ) << " bytes." << endl;
}
};

int main ( ) {

//Until C++14
MyClass<int, double> obj1;
obj1.printSizeOfDataTypes( );

//New syntax in C++17
MyClass obj2( 1, 10.56 );

return 0;
}

The preceding code can be compiled and the output can be viewed with the following commands:

g++-7 main.cpp -std=c++17
./a.out

The output of the preceding program is as follows:

Values in integer vectors are ...
1 2 3 4 5

Values in double vectors are ...
1.5 2.5 3.5
主站蜘蛛池模板: 巍山| 垣曲县| 卢氏县| 平南县| 承德县| 华阴市| 田东县| 克山县| 富民县| 开平市| 托里县| 永清县| 磐石市| 清镇市| 松桃| 鸡西市| 抚松县| 临邑县| 丰顺县| 吴旗县| 鄂托克前旗| 商南县| 江北区| 彩票| 大埔区| 台中市| 阿拉善左旗| 宜川县| 莱西市| 特克斯县| 周宁县| 福州市| 镇原县| 民丰县| 尚义县| 西安市| 新晃| 新竹市| 南宁市| 江津市| 泗洪县|