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

使用 std::enable_if 作為模板時(shí)的默認(rèn)模板參數(shù).參數(shù)

Default template argument when using std::enable_if as templ. param.: why OK with two template functions that differ only in the enable_if parameter?(使用 std::enable_if 作為模板時(shí)的默認(rèn)模板參數(shù).參數(shù):為什么兩個(gè)模板函數(shù)
本文介紹了使用 std::enable_if 作為模板時(shí)的默認(rèn)模板參數(shù).參數(shù):為什么兩個(gè)模板函數(shù)只在 enable_if 參數(shù)上不同?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

在 cppreference 的 std::enable_if 的語(yǔ)言參考中 包含以下注釋

In the language reference of std::enable_if at cppreference the following note is included

注意事項(xiàng)

一個(gè)常見的錯(cuò)誤是聲明兩個(gè)不同的函數(shù)模板僅在它們的默認(rèn)模板參數(shù)中. 這是非法的,因?yàn)槟J(rèn)模板參數(shù)不是函數(shù)模板的一部分簽名,并聲明兩個(gè)不同的函數(shù)模板相同的簽名是非法的.

A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal.

在下面示例中的模板函數(shù)中,在我看來,這種情況發(fā)生了.即,兩個(gè)模板函數(shù) onlyForDerivedObjects(...) 似乎(對(duì)我而言)僅在默認(rèn)模板參數(shù)上有所不同.我意識(shí)到我在這里遺漏了一些東西,希望有人可以向我解釋這一點(diǎn),或者為我指明方向,我可以找到自己的頓悟.

In the template functions in example below, it seems to me that this situation occurs. I.e., the two template functions onlyForDerivedObjects(...) seem (to me) to differ only by their default template arguments. I realize I am missing something here, and hopefully someone can explain this to me, or point me in the direction to where I might find an epiphany for myself.

  • 問題: W.r.t.上面的引用,為什么下面的例子編譯和運(yùn)行良好:當(dāng)我認(rèn)為它產(chǎn)生具有兩個(gè)模板的情況時(shí),我是否錯(cuò)誤分類了下面模板函數(shù)中的 typename std::enable_if ... 部分僅在默認(rèn)模板參數(shù)方面不同的函數(shù)?
  • Question: W.r.t. the quote above, why do the example below compile and run fine: do I misclassify the typename std::enable_if ... part in the template functions below when I consider it to yield a situation with two template functions which differ only in their default template argument?
  • 現(xiàn)場(chǎng)演示

基類和派生類:

class BaseA
{
public:
  int getInt() const { return 21; };
};

class DerivedA : public BaseA {};

class BaseB
{
public:
  int getAnotherInt() const { return 33; };
};

class DerivedB : public BaseB {};

具有以下模板函數(shù)

/* template functions that, seemingly, only differ in their
   default template arguments? */
template< class T,
          typename std::enable_if<std::is_base_of<BaseA, T>::value>::type* = nullptr >
int onlyForDerivedObjects(const T& obj)
{
  return 2*obj.getInt();
}

template< class T,
          typename std::enable_if<std::is_base_of<BaseB, T>::value>::type* = nullptr >
int onlyForDerivedObjects(const T& obj)
{
  return 3*obj.getAnotherInt();
}

編譯并運(yùn)行良好(g++ -Wall -std=c++11 ..., g++ 4.9.3)

compiles and runs fine (g++ -Wall -std=c++11 ..., g++ 4.9.3)

#include <iostream>
#include <type_traits>

/* ... classes and template functions as above */

/* template argument deduction seems to work fine */
int main()
{
  DerivedA* objA = new DerivedA();
  DerivedB* objB = new DerivedB();

  std::cout << onlyForDerivedObjects(*objA) << std::endl; // 42
  std::cout << onlyForDerivedObjects(*objB) << std::endl; // 99

  return 0;
}

推薦答案

注意事項(xiàng)

一個(gè)常見的錯(cuò)誤是聲明兩個(gè)僅在默認(rèn)模板參數(shù)上不同的函數(shù)模板.這是非法的,因?yàn)槟J(rèn)模板參數(shù)不是函數(shù)模板簽名的一部分,并且聲明具有相同簽名的兩個(gè)不同函數(shù)模板是非法的.

A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal.

您的函數(shù)不僅在默認(rèn)模板參數(shù)方面有所不同,而且在模板參數(shù)方面也有所不同,因此具有不同的簽名.

Your functions don't differ only in their default template arguments, they differ in their template parameters, so have different signatures.

在這兩種情況下,默認(rèn)模板參數(shù)都是nullptr,但第二個(gè)模板參數(shù)在每種情況下都不同.

In both cases the default template argument is nullptr, but the second template parameter is different in each case.

這篇關(guān)于使用 std::enable_if 作為模板時(shí)的默認(rèn)模板參數(shù).參數(shù):為什么兩個(gè)模板函數(shù)只在 enable_if 參數(shù)上不同?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(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++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 91精品国产乱码久久久久久久久 | 日韩欧美视频网站 | 欧洲精品码一区二区三区免费看 | 国产日韩精品在线 | 爱爱视频在线观看 | 成人免费小视频 | 91在线观看视频 | 欧洲av在线 | 欧美日韩精品一区二区 | 亚洲欧美日韩久久 | 欧美精品一区二区三区蜜臀 | 欧美电影在线 | 中文字幕一级毛片视频 | 天天干人人 | 久久综合一区二区三区 | 亚洲一区精品视频 | 久久香蕉网 | 日韩精品一区二区三区四区视频 | 九九热这里只有精品在线观看 | 一区二区免费在线 | 日韩在线一区二区三区 | 三级在线视频 | 久久网站免费视频 | 欧美一级二级在线观看 | 亚洲色欧美另类 | 在线免费观看毛片 | 91免费电影 | 欧美一级视频 | 蜜桃臀av一区二区三区 | 91精品在线看 | 久久久夜夜夜 | 国产精品中文字幕一区二区三区 | 91精品国产综合久久香蕉麻豆 | av超碰| 一级黄色绿像片 | 欧美日韩国产精品激情在线播放 | 天天碰夜夜操 | 亚洲精品视频二区 | 性大毛片视频 | 亚洲美女视频 | 国产一区二区影院 |