久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 中帶有 typedef 和模板的常量引用

constant references with typedef and templates in c++(C++ 中帶有 typedef 和模板的常量引用)
本文介紹了C++ 中帶有 typedef 和模板的常量引用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我聽說臨時對象只能分配給常量引用.

I heard the temporary objects can only be assigned to constant references.

但是這段代碼報錯

#include <iostream.h>    
template<class t>
t const& check(){
  return t(); //return a temporary object
}    
int main(int argc, char** argv){

const int &resCheck = check<int>(); /* fine */
typedef int& ref;
const ref error = check<int>(); / *error */
return 0;
}

得到的錯誤是類型'int&'的引用初始化無效from 'const int' 類型的表達式

推薦答案

這個:

typedef int& ref;
const ref error;

沒有做你認為它會做的事情.考慮一下:

Doesn't do what you think it does. Consider instead:

typedef int* pointer;
typedef const pointer const_pointer;

const_pointer 的類型是int* const不是 const int *.也就是說,當您說 const T 時,您是在說創建一個類型,其中 T 是不可變的";所以在前面的例子中,指針(不是被指點者)是不可變的.

The type of const_pointer is int* const, not const int *. That is, when you say const T you're saying "make a type where T is immutable"; so in the previous example, the pointer (not the pointee) is made immutable.

引用不能是constvolatile.這:

int& const x;

沒有意義,因此向引用添加 cv 限定符沒有任何效果.

is meaningless, so adding cv-qualifiers to references has no effect.

因此,error 的類型為 int&.您不能為其分配 const int&.

Therefore, error has the type int&. You cannot assign a const int& to it.

您的代碼中存在其他問題.例如,這肯定是錯誤的:

There are other problems in your code. For example, this is certainly wrong:

template<class t>
t const& check()
{
    return t(); //return a temporary object
}

您在這里所做的是返回對臨時對象的引用當函數返回時該對象結束其生命周期.也就是說,如果你使用它,你會得到未定義的行為,因為在引用處沒有對象.這并不比:

What you're doing here is returning a reference to a temporary object which ends its lifetime when the function returns. That is, you get undefined behavior if you use it because there is no object at the referand. This is no better than:

template<class t>
t const& check()
{
    T x = T();
    return x; // return a local...bang you're dead
}    

更好的測試是:

template<class T>
T check()
{
    return T();
}

函數的返回值是臨時的,所以你仍然可以測試你確實可以將臨時對象綁定到常量引用.

The return value of the function is a temporary, so you can still test that you can indeed bind temporaries to constant references.

這篇關于C++ 中帶有 typedef 和模板的常量引用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 色成人免费网站 | 久久精品亚洲精品国产欧美 | 亚洲精品久久久久久一区二区 | a在线观看 | 91久久久精品国产一区二区蜜臀 | 国产在线不卡 | 亚洲欧美日韩在线不卡 | 91.色 | 亚洲网视频 | 国产成人精品免费视频大全最热 | 网站黄色在线 | 欧美h视频 | 国产高清视频一区 | 综合二区| 黄色大片毛片 | 野狼在线社区2017入口 | 久艹网站| 神马久久春色视频 | 天天躁日日躁狠狠很躁 | 毛片免费在线 | 亚洲一区在线播放 | 一区在线视频 | 在线免费观看视频你懂的 | 亚洲一区久久 | 久久成人精品视频 | 国产精品日女人 | 日韩国产一区二区三区 | 久久亚洲一区二区三区四区 | 日日噜噜噜夜夜爽爽狠狠视频97 | 91精品久久久久久久久 | 欧美日韩亚洲视频 | 国产亚洲精品久久久久动 | 亚洲啊v在线 | 中文字幕一区二区三区不卡在线 | 一区二区三区免费 | 国产欧美精品一区二区 | 久久99一区二区 | 精品中文字幕一区二区三区 | 免费国产视频 | 欧美日韩国产在线观看 | 波霸ol一区二区 |