- Mastering C++ Programming
- Jeganathan Swaminathan
- 180字
- 2021-07-02 18:28:46
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
推薦閱讀
- Learning Cython Programming
- Microsoft Dynamics 365 Extensions Cookbook
- ASP.NET Core Essentials
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- INSTANT Django 1.5 Application Development Starter
- 零基礎趣學C語言
- 常用工具軟件立體化教程(微課版)
- 軟件測試教程
- Windows Embedded CE 6.0程序設計實戰
- 零基礎學Kotlin之Android項目開發實戰
- Creating Data Stories with Tableau Public
- SQL Server 2016 從入門到實戰(視頻教學版)
- Training Systems Using Python Statistical Modeling
- Instant GLEW
- 深入淺出 HTTPS:從原理到實戰