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

使用不同的 enable_if 條件選擇成員函數(shù)

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

問題描述

限時送ChatGPT賬號..

我正在嘗試根據(jù)類模板參數(shù)確定調(diào)用哪個版本的成員函數(shù).我試過這個:

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 有效,因為 模板參數(shù)的替換導(dǎo)致錯誤,因此替換從重載決議集中被刪除,編譯器只考慮其他可行的重載.

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.

在您的示例中,實例化成員函數(shù)時沒有發(fā)生替換,因為模板參數(shù) T 那時已經(jīng)知道.實現(xiàn)您正在嘗試的最簡單方法是創(chuàng)建一個默認為 T 的虛擬模板參數(shù),并使用它來執(zhí)行 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 在評論中提到的,原始示例留下了用戶為成員函數(shù)明確指定模板參數(shù)并得到錯誤結(jié)果的可能性.以下應(yīng)防止編譯成員函數(shù)的顯式特化.

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;
  }
};

這篇關(guān)于使用不同的 enable_if 條件選擇成員函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guā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++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
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?(取消引用已刪除的指針總是會導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 日韩中文av | 亚洲一区在线看 | 国产视频一区在线 | 黄色小视频免费 | 色爱天堂 | 亚洲免费视频网站 | av国产精品| 91av视频| 日韩精品久久久 | 黄色国产网站 | 亚欧洲精品在线视频免费观看 | 久久视频免费 | 色综合久久综合 | 亚洲综合三区 | 黄色一级视频网站 | 欧美天堂 | 在线观看黄色av | 欧美日韩一区二区三区视频 | 亚洲精品911 | 亚洲精品视频在线播放 | 欧美福利一区二区 | 伊人干综合 | 亚洲第一毛片 | 中文字幕超清在线观看 | 青青草av| 欧美性猛交99久久久久99按摩 | 69免费视频| 欧美一级黄色录像 | 自拍偷拍专区 | 一区二区三区免费在线观看 | 国产精品九九 | 中国少妇xxxxhd做受 | 91亚色视频 | 国产成人午夜精品 | 久久久久免费 | 国产毛片在线 | 成人在线小视频 | 色噜噜狠狠一区二区三区果冻 | 国产成人精品一区二区三区福利 | 青青草视频网站 | 欧美精品一二区 |