当前位置:网站首页>Decltype usage introduction

Decltype usage introduction

2022-06-24 08:00:00 GarryLau

Want to infer from the expression the type of variable to be defined , But I don't want to evaluate the expression , You can use decltype
Grammar is :delctype( expression ), Where the expression can be Variable 、 function 、 Array etc. .

#include <typeinfo>
#include <iostream>

namespace test_decltype {
    
	double onlyDeclartionFunc();

    auto main() -> int {
    
		std::cout << "testing decltype..." << std::endl;
		
		/*  Expressions are functions  */
		decltype(onlyDeclartionFunc()) sum = 34;   //  Use decltype When inferring a type from a function type, you can only require the function to have a declaration , Functions are not required to be defined 
		std::cout << "type(sum) is: " << typeid(sum).name() << std::endl;  // double
		
		/***************************************************************************/

		float i = 3.4f;
		decltype(i) a = 52;
		std::cout << "type(a) is: " << typeid(a).name() << std::endl;     // float
		
		//  Use decltype Will return the real type of the variable ( Include const And quotes ), This is related to auto There's a difference 
		const int ci = 0;        // const int
		const int &cj = ci;      // const int &
		decltype(ci) b = 9;      // const int
		// b = 10; // error C3892: “b”:  Constant cannot be assigned 
		decltype(cj) c = b;      // const int &
		// c = ci; // error C3892: “c”:  Constant cannot be assigned 
		decltype(cj) d = 9;     // const int &
		// decltype(cj) e; // error C2530: “e”:  Reference... Must be initialized 
		std::cout << "type(ci) is: " << typeid(ci).name() << std::endl;  // const int(ps: The compiler will not output with const, The same below )
		std::cout << "type(cj) is: " << typeid(cj).name() << std::endl;  // const int &
		std::cout << "type(b) is: " << typeid(b).name() << std::endl;    // const int
		std::cout << "type(c) is: " << typeid(c).name() << std::endl;    // const int &
		std::cout << "type(d) is: " << typeid(d).name() << std::endl;    // const int &
		
		/***************************************************************************/
		
		// decltype( expression ) Infer several cases of reference types :
		// 1.  The expression itself is a reference ;
		// 2.  An expression is a dereference of a pointer ;
		// 3.  Expression parenthesized ;
		int j = 0;
		int &k = j;
		int *p = &j;
		
		std::cout << "Original j, 0 == " << j << std::endl;
		
		decltype(k) f = k;     // f yes j References to ( The expression itself is a reference )
		f = 1;
		std::cout << "f is j's reference, 1 == " << j << std::endl;
		
		decltype(*p) g = j;    // g yes j References to ( An expression is a dereference of a pointer )
		g = 2;
		std::cout << "g is j's reference, 2 == " << j << std::endl;
		
		decltype((j)) h = j;   // h yes j References to ( Expression parenthesized )
		h = 3;
		std::cout << "h is j's reference, 3 == " << j << std::endl;
		
		decltype(k+0) m = k;   // m yes int, No int&, because k+0 yes int type 
		m = 4;
		std::cout << "m is not j's reference, 4 != " << j << std::endl;
        
        //  Use... For arrays decltype** The result is the array type 
        int arr[] = {
    3,4,5};
        // decltype(arr) crr = {5,6,7,8,9}; // error: too many initializers for 'int [3]'
        decltype(arr) drr = {
    5,6,7};           //  Be careful , The number of array elements is part of the array type 
        std::cout << "type(drr) is: " << typeid(drr).name() << std::endl;   // int [3]
		
		/***************************************************************************/
		
		std::cout << "------------------------------" << std::endl;
        return 0;
    }
}

Output of the above program :

testing decltype...
type(sum) is: double
type(a) is: float
type(ci) is: int
type(cj) is: int
type(b) is: int
type(c) is: int
type(d) is: int
Original j, 0 == 0
f is j's reference, 1 == 1 g is j's reference, 2 == 2
h is j's reference, 3 == 3 m is not j's reference, 4 != 3
type(drr) is: int [3]

Use... For arrays decltype The result is the array type , And auto Different

//  Use... For arrays decltype
int arr[] = {
    3,4,5};
       
// decltype(arr) crr = {5,6,7,8,9}; // error: too many initializers for 'int [3]'
decltype(arr) drr = {
    5,6,7};           //  Be careful , The number of array elements is part of the array type 
std::cout << "type(drr) is: " << typeid(drr).name() << std::endl;   // int [3]
原网站

版权声明
本文为[GarryLau]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240421274597.html