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

使用 enable_if 選擇類構造函數

Select class constructor using enable_if(使用 enable_if 選擇類構造函數)
本文介紹了使用 enable_if 選擇類構造函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

考慮以下代碼:

#include <iostream>
#include <type_traits>

template <typename T>
struct A {
    int val = 0;

    template <class = typename std::enable_if<T::value>::type>
    A(int n) : val(n) {};
    A(...) { }

    /* ... */
};

struct YES { constexpr static bool value = true; };
struct NO { constexpr static bool value = false; };

int main() {
    A<YES> y(10);
    A<NO> n;
    std::cout << "YES: " << y.val << std::endl
              << "NO:  " << n.val << std::endl;
}

我想有選擇地定義構造函數 A::A(int) 僅用于使用 enable_if 的某些類型.對于所有其他類型,默認構造函數 A::A(...) 應該是替換失敗時編譯器的默認情況.然而,這對我來說很有意義,編譯器(gcc 版本 4.9.0 20130714)仍在抱怨

I want to selectively define constructor A::A(int) only for some types using enable_if. For all other types there is default constructor A::A(...) which should be the default case for compiler when substitution fails. However this makes sense for me compiler (gcc version 4.9.0 20130714) is still complaining

sfinae.cpp:在結構 A"的實例化中:sfinae.cpp:19:11:
從這里需要 sfinae.cpp:9:5: 錯誤:
中沒有名為type"的類型'struct std::enable_if'
A(int n) : val(n) {};

sfinae.cpp: In instantiation of 'struct A': sfinae.cpp:19:11:
required from here sfinae.cpp:9:5: error: no type named 'type' in
'struct std::enable_if'
A(int n) : val(n) {};

這樣的事情對構造函數來說是可能的嗎?這是否可以與另一個構造函數(復制構造函數和移動構造函數)一起使用?

Is something like this possible for constructor? Is this possible with another constructor(s) (copy-constructor and move-constructor)?

推薦答案

With C++20

您只需將 requires 添加到模板即可實現:

With C++20

You can achieve that simply by adding requires to the template:

template <typename U = T> requires U::value
A(int n) : val(n) { }

requires 子句獲取一個 常量表達式,其計算結果為 truefalse> 因此決定是否在重載決議中考慮此方法,如果 requires 子句為真,否則忽略它.

The requires clause gets a constant expression that evaluates to true or false deciding thus whether to consider this method in the overload resolution, if the requires clause is true, or ignore it otherwise.

代碼:https://godbolt.org/z/CKTDFE

這篇關于使用 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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 亚洲欧美在线视频 | 亚洲精品99 | 久久婷婷av | 国产高清在线观看 | 亚洲成人av | 精品国产乱码 | 中文在线一区二区 | 一区影视| 欧美白人做受xxxx视频 | 99中文字幕 | 中文字幕视频在线观看 | 国产免费福利小视频 | 色婷婷国产精品综合在线观看 | a精品视频 | 狠狠亚洲 | 日本成人免费网站 | 日韩有码一区 | 国产精品一二三区 | 日韩一区二区三区在线观看 | 欧美一级大片免费看 | 国产精品欧美一区二区三区不卡 | 97免费在线视频 | 久久久女女女女999久久 | 亚洲成人自拍 | 玖玖精品视频 | 91综合网| 久久久精品影院 | 成人一区二区三区 | 欧美精品一区二区免费视频 | 精品一区二区视频 | 涩涩鲁亚洲精品一区二区 | 黄色片视频免费 | 女同久久| 免费国产成人av | 精品国产一区二区三区观看不卡 | 国产精品成人一区二区三区夜夜夜 | 情侣黄网站免费看 | 国产精品成人一区二区三区 | 国产精品有限公司 | 中文字幕一区二区三区乱码图片 | 中文字幕视频在线免费 |