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

使用不同的 enable_if 條件選擇成員函數

Selecting a member function using different enable_if conditions(使用不同的 enable_if 條件選擇成員函數)
本文介紹了使用不同的 enable_if 條件選擇成員函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試根據類模板參數確定調用哪個版本的成員函數.我試過這個:

I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this:

#include <iostream>
#include <type_traits>

template<typename T>
struct Point
{
  void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0)
  {
    std::cout << "T is int." << std::endl;
  }

  void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0)
  {
    std::cout << "T is not int." << std::endl;
  }
};

int main()
{
  Point<int> intPoint;
  intPoint.MyFunction();

  Point<float> floatPoint;
  floatPoint.MyFunction();
}

我認為這是說如果 T 是 int,則使用第一個 MyFunction,如果 T 不是 int,則使用第二個 MyFunction,但是我收到編譯器錯誤,說錯誤:'struct std:: 中沒有名為'type'的類型:enable_if'".誰能指出我在這里做錯了什么?

which I thought is saying "use the first MyFunction if T is int, and use the second MyFunction if T is not int, but I get compiler errors saying "error: no type named ‘type’ in ‘struct std::enable_if’". Can anyone point out what I am doing wrong here?

推薦答案

enable_if 有效,因為 模板參數的替換導致錯誤,因此替換從重載決議集中被刪除,編譯器只考慮其他可行的重載.

enable_if works because the substitution of a template argument resulted in an error, and so that substitution is dropped from the overload resolution set and only other viable overloads are considered by the compiler.

在您的示例中,實例化成員函數時沒有發生替換,因為模板參數 T 那時已經知道.實現您正在嘗試的最簡單方法是創建一個默認為 T 的虛擬模板參數,并使用它來執行 SFINAE.

In your example, there is no substitution occurring when instantiating the member functions because the template argument T is already known at that time. The simplest way to achieve what you're attempting is to create a dummy template argument that is defaulted to T and use that to perform SFINAE.

template<typename T>
struct Point
{
  template<typename U = T>
  typename std::enable_if<std::is_same<U, int>::value>::type
    MyFunction()
  {
    std::cout << "T is int." << std::endl;
  }

  template<typename U = T>
  typename std::enable_if<std::is_same<U, float>::value>::type
    MyFunction()
  {
    std::cout << "T is not int." << std::endl;
  }
};

<小時>


正如 HostileFork 在評論中提到的,原始示例留下了用戶為成員函數明確指定模板參數并得到錯誤結果的可能性.以下應防止編譯成員函數的顯式特化.

As HostileFork mentions in the comments, the original example leaves the possibility of the user explicitly specifying template arguments for the member functions and getting an incorrect result. The following should prevent explicit specializations of the member functions from compiling.

template<typename T>
struct Point
{
  template<typename... Dummy, typename U = T>
  typename std::enable_if<std::is_same<U, int>::value>::type
    MyFunction()
  {
    static_assert(sizeof...(Dummy)==0, "Do not specify template arguments!");
    std::cout << "T is int." << std::endl;
  }

  template<typename... Dummy, typename U = T>
  typename std::enable_if<std::is_same<U, float>::value>::type
    MyFunction()
  {
    static_assert(sizeof...(Dummy)==0, "Do not specify template arguments!");
    std::cout << "T is not int." << std::endl;
  }
};

這篇關于使用不同的 enable_if 條件選擇成員函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 | 国产精品99免费视频 | 91精品午夜窝窝看片 | 亚洲精品在线看 | 久久999 | 免费在线观看黄网站 | 国产精品xxxx| 成人av免费播放 | 婷婷去俺也去 | 久操亚洲| 亚洲高清在线视频 | 农村妇女毛片精品久久久 | 国产一区二区三区在线 | 久久国产视频一区 | 少妇一级淫片免费播放 | 成人在线观看黄 | 337p日本欧洲亚洲大胆精蜜臀 | 久在线精品视频 | 国产欧美精品一区二区三区 | 韩日精品一区 | 米奇7777狠狠狠狠视频 | 精品久久久久久久久久 | 亚洲女人的天堂 | 国产激情网站 | 亚洲福利网站 | 亚洲视频一区二区三区 | 中文字幕一区二区三区乱码图片 | 一区中文| 欧美日韩一区在线 | 亚洲精品成人网 | 亚洲精品视频久久 | 91就要激情 | 一级毛片免费视频 | 国产精品久久欧美久久一区 | 91久久精品 | 国产精品99久久久久久人 | 免费色网址 | 野狼在线社区2017入口 | 色橹橹欧美在线观看视频高清 | 久草免费在线视频 | 99精品99久久久久久宅男 |