当前位置:网站首页>Declval of template in generic programming

Declval of template in generic programming

2022-07-06 18:03:00 Hair like snow ty

std::declval yes c++11 A function template that appears in the standard , This function template looks strange , Because it has no function body ( It didn't come true , Only the statement , It was designed on purpose ), So it can't be called , It is generally used for decltype,sizeof And other keywords are used together for class type derivation 、 Occupy memory space calculation, etc . Its source code is as follows :

template<typename T>
add_ravalue_reference_t<T> declval() noexcept;

add_ravalue_reference( Note that there is no _t,add_ravalue_reference_t Is the alias template corresponding to this type ) yes c++ A class template provided in the standard library , Its function is to introduce a type , Return the right value reference type of this type . Such as :
(1) Pass in int type , The return is int && type ;
(2) Pass in int & type , Then it returns int &, The reference folding rule is applied here .
(3) Pass in int && type , Back again int && type , The reference folding rule is also used here .
You can see it in the source code declval The function of is to return a certain type T To the right of , Whether or not the type has a default constructor , Or whether this type can create objects . These actions are completed at compile time , So also known as std::declval Is a compile time tool .

class cTest
{
    
public:
	cTest(int a)
	{
    
		cout << " Constructors \n";
	}
	double myfunc()
	{
    
		cout << "myfunc\n";
		return 1.1;
	}
};

problem : How to get myfunc() The return type of this member function double? The traditional approach is as follows :

int main()
{
    	
	cTest myobj(1);
	cout << typeid(decltype(myobj.myfunc())).name() << endl;
	
	system("pause");
	return 0;
}

result :
 Insert picture description here
It can be seen from the results , In order to obtain myfunc The return type of , You must create a class cTest The object of ; that , You can think more deeply , Create a class myobj so much trouble , Also provide arguments , If you don't create objects of classes , Still want myfunc() The return type of the member function , Can this idea be realized ? Certainly. ~

int main()
{
    	
	
	cout << typeid(decltype(declval<cTest>().myfunc())).name() << endl;
	
	system("pause");
	return 0;
}

result :

 Insert picture description here
From the results , There are no objects that create classes , No call myfunc Member functions , But I really got myfunc The return type of .
declval Function summary :
(1) From the perspective of type conversion , Convert any type to an R-value reference type .
(2) From the perspective of hypothetical creation of certain types of objects , coordination decltype, Make decltype This type does not have to be passed in the expression ( Generally refers to a class type ) Constructor for , You can use the member functions of this class .
Be careful ,std::declval Cannot be called , Nor can you create any objects . however std::declval The ability to create objects without , To achieve the effect of creating an object of this type .

原网站

版权声明
本文为[Hair like snow ty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061004572902.html