最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文詳解C++11中decltype的使用

 更新時(shí)間:2023年07月02日 16:36:34   作者:fengbingchun  
這篇文章主要為大家分享了C++11中decltype關(guān)鍵字的使用示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments. Or,use auto and decltype to declare a template function that wraps a call to another function, and then returns the return type of the wrapped function.

The compiler uses the following rules to determine the type of the expression parameter:

(1)、If the expression parameter is an identifier or a class member access, decltype(expression) is the type of the entity named by expression. If there is no such entity or the expression parameter names a set of overloaded functions,the compiler yields an error message.

(2)、If the expression parameter is a call to a function or an overloaded operator function, decltype(expression) is the return type of the function. Parentheses around an overloaded operator are ignored.

(3)、If the expression parameter is an rvalue, decltype(expression) is the type of expression. If the expression parameter is an lvalue, decltype(expression) is an lvalue reference to the type of expression.

decltype is useful when declaring types that are difficult or impossible to declare using standard notation, like lambda-related types or types that depend on template parameters.

decltype is a standard C++11 feature. It is an "operator" which takes an expression and returns a type.

decltype與auto關(guān)鍵字一樣,用于進(jìn)行編譯時(shí)類(lèi)型推導(dǎo),不過(guò)它與auto是有一些區(qū)別的。decltype的類(lèi)型推導(dǎo)并不是像auto一樣是從變量聲明的初始化表達(dá)式獲得變量的類(lèi)型,而是總是以一個(gè)普通表達(dá)式作為參數(shù),返回該表達(dá)式的類(lèi)型,而且decltype并不會(huì)對(duì)表達(dá)式進(jìn)行求值。

decltype關(guān)鍵字用于查詢(xún)表達(dá)式的類(lèi)型,并不會(huì)對(duì)表達(dá)式進(jìn)行求值。decltype的作用是獲得一個(gè)變量或表達(dá)式的類(lèi)型。decltype 不會(huì)執(zhí)行表達(dá)式而auto會(huì),decltype僅僅推論一下表達(dá)式的類(lèi)型。

對(duì)于decltype( e )而言,其判別結(jié)果受以下條件的影響:

(1)、如果e是一個(gè)標(biāo)識(shí)符或者類(lèi)成員的訪問(wèn)表達(dá)式,則decltype(e)就是e所代表的實(shí)體的類(lèi)型。如果沒(méi)有這種類(lèi)型或者e是一個(gè)重載函數(shù)集,那么程序是錯(cuò)誤的;

(2)、如果e是一個(gè)函數(shù)調(diào)用或者一個(gè)重載操作符調(diào)用(忽略e外面的括號(hào)),那么decltype(e)就是該函數(shù)的返回類(lèi)型;

(3)、如果e不屬于以上所述的情況,則假設(shè)e的類(lèi)型是 T:當(dāng)e是一個(gè)左值時(shí),decltype(e)就是T&;否則(e是一個(gè)右值),decltype(e)是T。

auto是為所有人準(zhǔn)備的,而decltype是提供給模板開(kāi)發(fā)者的。

在C++中,decltype作為操作符,用于查詢(xún)表達(dá)式的數(shù)據(jù)類(lèi)型。decltype在C++11標(biāo)準(zhǔn)制定時(shí)引入,主要是為泛型編程而設(shè)計(jì),以解決泛型編程中,由于有些類(lèi)型由模板參數(shù)決定,而難以表示的問(wèn)題。

下面是從其他文章中copy的測(cè)試代碼,詳細(xì)內(nèi)容介紹可以參考對(duì)應(yīng)的reference:

#include "decltype.hpp"
#include <iostream>
#include <string>
#include <utility>
#include <iomanip>
//
// reference: http://en.cppreference.com/w/cpp/language/decltype
struct A { double x; };
const A* a = new A{ 0 };
decltype(a->x) y;       // type of y is double (declared type)
decltype((a->x)) z = y; // type of z is const double& (lvalue expression)
template<typename T, typename U>
auto add(T t, U u) -> decltype(t + u); // return type depends on template parameters
int test_decltype1()
{
	int i = 33;
	decltype(i) j = i * 2;
	std::cout << "i = " << i << ", " << "j = " << j << '\n';
	auto f = [](int a, int b) -> int
	{
		return a * b;
	};
	decltype(f) g = f; // the type of a lambda function is unique and unnamed
	i = f(2, 2);
	j = g(3, 3);
	std::cout << "i = " << i << ", " << "j = " << j << '\n';
	return 0;
}
///
// reference: https://msdn.microsoft.com/zh-cn/library/dd537655.aspx
template<typename T1, typename T2>
auto Plus(T1&& t1, T2&& t2) ->
decltype(std::forward<T1>(t1) +std::forward<T2>(t2))
{
	return std::forward<T1>(t1) +std::forward<T2>(t2);
}
class X
{
	friend X operator+(const X& x1, const X& x2)
	{
		return X(x1.m_data + x2.m_data);
	}
public:
	X(int data) : m_data(data) {}
	int Dump() const { return m_data; }
private:
	int m_data;
};
int test_decltype2()
{
	// Integer 
	int i = 4;
	std::cout << "Plus(i, 9) = " << Plus(i, 9) << std::endl;
	// Floating point
	float dx = 4.0;
	float dy = 9.5;
	std::cout << std::setprecision(3) << "Plus(dx, dy) = " << Plus(dx, dy) << std::endl;
	// String
	std::string hello = "Hello, ";
	std::string world = "world!";
	std::cout << Plus(hello, world) << std::endl;
	// Custom type
	X x1(20);
	X x2(22);
	X x3 = Plus(x1, x2);
	std::cout << "x3.Dump() = " << x3.Dump() << std::endl;
	return 0;
}
///
// reference: http://thbecker.net/articles/auto_and_decltype/section_06.html
struct S {
	S(){ m_x = 42; }
	int m_x;
};
int x;
const int cx = 42;
const int& crx = x;
const S* p = new S();
// x is declared as an int: x_type is int.
typedef decltype(x) x_type;
// auto also deduces the type as int: a_ is an int.
auto a_ = x;
// cx is declared as const int: cx_type is const int.
typedef decltype(cx) cx_type;
// auto drops the const qualifier: b is int.
auto b = cx;
// crx is declared as const int&: crx_type is const int&.
typedef decltype(crx) crx_type;
// auto drops the reference and the const qualifier: c is an int.
auto c = crx;
// S::m_x is declared as int: m_x_type is int
// Note that p->m_x cannot be assigned to. It is effectively
// constant because p is a pointer to const. But decltype goes
// by the declared type, which is int.
typedef decltype(p->m_x) m_x_type;
// auto sees that p->m_x is const, but it drops the const
// qualifier. Therefore, d is an int.
auto d = p->m_x;

GitHub: https://github.com/fengbingchun/Messy_Test

到此這篇關(guān)于一文詳解C++11中decltype的使用的文章就介紹到這了,更多相關(guān)C++11 decltype內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c++ 構(gòu)造函數(shù)中調(diào)用虛函數(shù)的實(shí)現(xiàn)方法

    c++ 構(gòu)造函數(shù)中調(diào)用虛函數(shù)的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇c++ 構(gòu)造函數(shù)中調(diào)用虛函數(shù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • C語(yǔ)言實(shí)現(xiàn)代碼雨效果

    C語(yǔ)言實(shí)現(xiàn)代碼雨效果

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)代碼雨效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C++?ROS與boost:bind()使用詳解

    C++?ROS與boost:bind()使用詳解

    boost::bind是標(biāo)準(zhǔn)庫(kù)函數(shù)std::bind1st和std::bind2nd的一種泛化形式,其可以支持函數(shù)對(duì)象、函數(shù)、函數(shù)指針、成員函數(shù)指針,并且綁定任意參數(shù)到某個(gè)指定值上或者將輸入?yún)?shù)傳入任意位置,本文重點(diǎn)介紹下C++?ROS與boost:bind(),感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • C++ 手把手教你實(shí)現(xiàn)可變長(zhǎng)的數(shù)組實(shí)現(xiàn)

    C++ 手把手教你實(shí)現(xiàn)可變長(zhǎng)的數(shù)組實(shí)現(xiàn)

    這篇文章主要介紹了C++ 手把手教你實(shí)現(xiàn)可變長(zhǎng)的數(shù)組實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • C++大小字母的轉(zhuǎn)換方式

    C++大小字母的轉(zhuǎn)換方式

    這篇文章主要介紹了C++大小字母的轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 詳解計(jì)數(shù)排序算法及C語(yǔ)言程序中的實(shí)現(xiàn)

    詳解計(jì)數(shù)排序算法及C語(yǔ)言程序中的實(shí)現(xiàn)

    技術(shù)排序算法與我們普通接觸的冒泡排序和快速排序等基于元素比較的算法不同,在編程中通過(guò)C語(yǔ)言的數(shù)組能夠清除地表達(dá)出來(lái),這里我們就來(lái)詳解計(jì)數(shù)排序算法及C語(yǔ)言程序中的實(shí)現(xiàn)
    2016-07-07
  • C語(yǔ)言中程序環(huán)境和預(yù)處理的詳細(xì)圖文講解

    C語(yǔ)言中程序環(huán)境和預(yù)處理的詳細(xì)圖文講解

    這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中程序環(huán)境和預(yù)處理的相關(guān)資料,我們寫(xiě)的C語(yǔ)言代碼,從運(yùn)行,到在屏幕上生成結(jié)果,經(jīng)歷了比較復(fù)雜的過(guò)程,需要的朋友可以參考下
    2023-02-02
  • 最新評(píng)論

    石家庄市| 大邑县| 绥阳县| 方城县| 大足县| 自贡市| 喀什市| 南澳县| 滁州市| 龙南县| 婺源县| 南川市| 元谋县| 沙洋县| 崇义县| 平定县| 神池县| 河北省| 咸阳市| 镇康县| 红原县| 中西区| 林周县| 宜州市| 福建省| 宁河县| 郑州市| 山丹县| 阳春市| 兰溪市| 自治县| 大悟县| 曲松县| 通许县| 江口县| 油尖旺区| 稻城县| 平阴县| 香格里拉县| 永宁县| 建瓯市|