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

使用 enable_if 選擇類構造函數(shù)

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

問題描述

限時送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;
}

我想有選擇地定義構造函數(shù) A::A(int) 僅用于使用 enable_if 的某些類型.對于所有其他類型,默認構造函數(shù) 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) {};

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

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

推薦答案

With C++20

您只需將 requires 添加到模板即可實現(xiàn):

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 選擇類構造函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(liá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++ - 如何從指向向量的指針訪問向量的內容?)
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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产精品永久免费视频 | 久久亚洲综合 | 久久福利网站 | 久久精品国产亚洲一区二区 | 久久久国产精品 | av手机在线免费观看 | 久久久久久国产精品免费免费狐狸 | h片免费在线观看 | 久久综合婷婷 | 久久久久久国产精品免费免费狐狸 | 美女张开腿露出尿口 | 久久久久久一区 | 免费在线成人 | 美日韩精品 | 国产精品亚洲视频 | 一区二区免费 | 亚洲精品一区中文字幕乱码 | 国产乱码久久久久久 | 亚洲在线一区二区三区 | 成年网站在线观看 | 黄片毛片免费看 | 日韩视频免费看 | 免费视频二区 | 欧美黄色精品 | 欧美精品1区 | 亚洲国产精品久久久 | 免费高潮视频95在线观看网站 | 欧美日韩高清在线一区 | 亚洲在线免费 | 欧美一级视频免费看 | 亚洲精品播放 | 亚洲第一av| 色黄爽| 天天操天天操 | 亚洲欧美综合精品久久成人 | 国产精品成人在线播放 | 欧美黑人狂野猛交老妇 | av在线视 | 亚洲乱码国产乱码精品精98午夜 | 欧美一区二区免费视频 | 羞羞色视频 |