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

為什么編譯器不能從默認參數推導出模板類型?

Why can#39;t the compiler deduce the template type from default arguments?(為什么編譯器不能從默認參數推導出模板類型?)
本文介紹了為什么編譯器不能從默認參數推導出模板類型?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我很驚訝下面的代碼會導致 無法推導出 T 的模板參數 錯誤:

I was surprised the following code resulted in a could not deduce template argument for T error:

struct foo
{
  template <typename T>
  void bar(int a, T b = 0.0f)
  {
  }
};

int main()
{
  foo a;
  a.bar(5);

  return 0;
}

調用 a.bar(5) 修復了這個問題.為什么編譯器不能從默認參數中推導出類型?

Calling a.bar<float>(5) fixes the issue. Why can't the compiler deduce the type from the default argument?

推薦答案

在 C++03 中,規范明確禁止使用默認參數來推導模板參數 (C++03 §14.8.2/17):

In C++03, the specification explicitly prohibits the default argument from being used to deduce a template argument (C++03 §14.8.2/17):

不能從函數默認參數的類型推導出模板類型參數.

A template type-parameter cannot be deduced from the type of a function default argument.

在 C++11 中,你可以為函數模板提供一個默認的模板參數:

In C++11, you can provide a default template argument for the function template:

template <typename T = float>
void bar(int a, T b = 0.0f) { }

不過,默認模板參數是必需的.如果未提供默認模板參數,則默認函數參數仍然不能用于模板參數推導.具體而言,以下適用 (C++11 14.8.2.5/5):

The default template argument is required, though. If the default template argument is not provided, the default function argument is still not usable for template argument deduction. Specifically, the following applies (C++11 14.8.2.5/5):

非推導的上下文是:

...

  • 在函數形參的形參類型中使用的模板形參,該形參有一個默認實參,該實參在正在執行自變量推導的調用中使用.

這篇關于為什么編譯器不能從默認參數推導出模板類型?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 日日夜夜天天 | 一区二区三区视频在线免费观看 | 日韩在线播放av | 欧美午夜在线 | 久久久久国产精品午夜一区 | 7777在线| 日本精品一区二区三区在线观看视频 | 国产精品久久久久9999鸭 | 在线四虎| 亚洲精品无 | 国产成人免费在线 | 亚洲一级淫片 | 精品久久久久久久久久久久久久 | 日韩视频成人 | 国产一区二区免费在线 | 国产偷久久一级精品60部 | 激情六月天 | 免费一二区 | 拍拍无遮挡人做人爱视频免费观看 | 国产色片在线 | 青青久在线视频 | 日日干夜夜操天天操 | 国产女人与拘做受免费视频 | 欧美美女二区 | 成人精品高清 | 亚洲精品久久久一区二区三区 | 97超碰人人 | 久久精品国产精品青草 | 成人免费福利 | 久久精品亚洲 | 中文字幕电影在线观看 | 午夜免费福利片 | 精品国产免费一区二区三区演员表 | 亚洲中午字幕 | 九九免费视频 | 久久国产秒 | 国产精品国产三级国产aⅴ浪潮 | 国产色婷婷久久99精品91 | 欧美激情亚洲天堂 | 亚洲国产成人精品女人久久久 | 国产激情一区二区三区 |