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

帶有包擴展的可變函數(shù)模板不在最后一個參數(shù)中

Variadic function template with pack expansion not in last parameter(帶有包擴展的可變函數(shù)模板不在最后一個參數(shù)中)
本文介紹了帶有包擴展的可變函數(shù)模板不在最后一個參數(shù)中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想知道為什么下面的代碼不能編譯:

I am wondering why the following code doesn't compile:

struct S
{
    template <typename... T>
    S(T..., int);
};

S c{0, 0};

此代碼無法同時使用 clang 和 GCC 4.8 進行編譯.這是 clang 的錯誤:

This code fails to compile with both clang and GCC 4.8. Here is the error with clang:

test.cpp:7:3: error: no matching constructor for initialization of 'S'
S c{0, 0};
  ^~~~~~~
test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    S(T..., int);
    ^

在我看來這應該可行,并且 T 應該被推導出為長度為 1 的包.

It seems to me that this should work, and T should be deduced to be a pack of length 1.

如果標準禁止做這樣的事情,有誰知道為什么?

If the standards forbids doing things like this, does anyone know why?

推薦答案

因為當一個函數(shù)形參包不是最后一個形參時,那么模板形參包就不能從中推導出來,模板實參推導會忽略它.

Because when a function parameter pack is not the last parameter, then the template parameter pack cannot be deduced from it and it will be ignored by template argument deduction.

因此將兩個參數(shù) 0, 0, int 進行比較,結果不匹配.

So the two arguments 0, 0 are compared against , int, yielding a mismatch.

這樣的推導規(guī)則需要涵蓋很多特殊情況(比如兩個參數(shù)包并排出現(xiàn)時會發(fā)生什么).由于參數(shù)包是 C++11 中的一個新特性,相應提案的作者在起草規(guī)則時比較保守.

Deduction rules like this need to cover many special cases (like what happens when two parameter packs appear next to each other). Since parameter packs are a new feature in C++11, the authors of the respective proposal drafted the rules conservatively.

請注意,如果沒有以其他方式推導出,尾隨模板參數(shù)包將為空.所以當你用一個參數(shù)調用構造函數(shù)時,事情就會起作用(注意這里模板參數(shù)包和函數(shù)參數(shù)包的區(qū)別.前者是拖尾的,后者不是).

Note that a trailing template parameter pack will be empty if it is not otherwise deduced. So when you call the constructor with one argument, things will work (notice the difference of template parameter pack and function parameter pack here. The former is trailing, the latter is not).

這篇關于帶有包擴展的可變函數(shù)模板不在最后一個參數(shù)中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
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?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 黄色av免费在线观看 | 一级特黄视频 | 色综合久久久久 | 黄色a一级片 | 黄色片中文字幕 | 快播少女爱欢乐 | 超碰99在线 | 日日爱视频 | 日韩一区二区三区在线播放 | 欧美福利视频 | 在线日韩欧美 | 欧美激情综合 | 五月婷婷在线视频 | 涩涩的视频 | 亚久久| 欧美精品三区 | 男女av在线| 久草免费在线观看视频 | 黄色成人免费网站 | 欧美激情视频网站 | 青青在线 | 国产精品高潮呻吟av | 日韩欧美久久 | 红桃视频成人 | 国产精品成人一区二区 | 黄色片在线免费观看 | 少妇高潮av久久久久久 | 午夜一级视频 | 婷婷狠狠爱 | 日本国产欧美 | 伊人久久精品 | 日韩欧美精品一区二区 | 日韩欧美视频 | 五月婷婷开心 | 亚洲精品资源 | 二区三区在线观看 | 亚洲第一免费视频 | 在线a | 毛片视频免费 | 激情都市亚洲 | 日韩三级在线播放 |