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

用 SFINAE 檢測 constexpr

Detecting constexpr with SFINAE(用 SFINAE 檢測 constexpr)
本文介紹了用 SFINAE 檢測 constexpr的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在升級一些 C++ 代碼以利用 C++11 中的新功能.我有一個 trait 類,其中有幾個函數(shù)返回基本類型,這些函數(shù)在大多數(shù)情況下(但并非總是)返回一個常量表達式.我想根據(jù)函數(shù)是否為 constexpr 來做不同的事情.我想出了以下方法:

template結(jié)構測試{模板靜態(tài) std::true_type do_call(int){ return std::true_type();}靜態(tài) std::false_type do_call(...){ 返回 std::false_type();}static bool call(){ return do_call(0);}};結(jié)構特征{靜態(tài) int f(){ 返回 15;}};結(jié)構特征{靜態(tài) constexpr int f(){ 返回 20;}};int main(){std::cout <<常規(guī):" <<測試<特質(zhì)>::call()<<std::endl;std::cout <<constexpr:" <<測試<ctrait>::call()<<std::endl;}

額外的 int/... 參數(shù)在那里,如果 SFINAE 之后兩個函數(shù)都可用,第一個被選擇重載解析.

使用 Clang 3.2 編譯和運行它顯示:

常規(guī):0常量表達式:1

所以這似乎有效,但我想知道代碼是否是合法的 C++11.特別是根據(jù)我的理解,SFINAE 的規(guī)則已經(jīng)改變.

解決方案

注意: 我在這里提出了一個問題,關于 OP 代碼是否實際有效.我在下面重寫的示例在任何情況下都可以使用.

<小時><塊引用>

但我想知道代碼是否合法 C++11

確實如此,雖然默認模板參數(shù)可能被認為有點不尋常.我個人更喜歡以下樣式,這類似于您(閱讀:我)編寫特征以檢查函數(shù)是否存在,僅使用非類型模板參數(shù)并省略 decltype:

#include 命名空間細節(jié){模板結(jié)構 sfinae_true : std::true_type{};模板sfinae_true<(T::f(), 0)>檢查(整數(shù));模板<類>std::false_type 檢查(...);}//細節(jié)::模板struct has_constexpr_f : decltype(detail::check<T>(0)){};

現(xiàn)場示例.

<小時>

講解時間~

您的原始代碼有效? 因為默認模板參數(shù)的實例化點是其函數(shù)模板的實例化點,在您的情況下,在 main 中,所以不能更早地替換它.

§14.6.4.1 [temp.point] p2

<塊引用>

如果函數(shù)模板 [...] 的調(diào)用方式使用了該函數(shù)模板 [...] 的默認參數(shù)的定義,則默認參數(shù)的實例化點是函數(shù)模板 [...].

在那之后,這只是通常的 SFINAE 規(guī)則.

<小時>

? 至少我是這么認為的,它在標準中并不完全明確.

I'm working on upgrading some C++ code to take advantage of the new functionality in C++11. I have a trait class with a few functions returning fundamental types which would most of the time, but not always, return a constant expression. I would like to do different things based on whether the function is constexpr or not. I came up with the following approach:

template<typename Trait>
struct test
{
    template<int Value = Trait::f()>
    static std::true_type do_call(int){ return std::true_type(); }

    static std::false_type do_call(...){ return std::false_type(); }

    static bool call(){ return do_call(0); }
};

struct trait
{
    static int f(){ return 15; }
};

struct ctrait
{
    static constexpr int f(){ return 20; }
};

int main()
{
   std::cout << "regular: " << test<trait>::call() << std::endl;
   std::cout << "constexpr: " << test<ctrait>::call() << std::endl;
}

The extra int/... parameter is there so that if both functions are available after SFINAE, the first one gets chosen by overloading resolution.

Compiling and running this with Clang 3.2 shows:

regular: 0
constexpr: 1

So this appears to work, but I would like to know if the code is legal C++11. Specially since it's my understanding that the rules for SFINAE have changed.

解決方案

NOTE: I opened a question here about whether OPs code is actually valid. My rewritten example below will work in any case.


but I would like to know if the code is legal C++11

It is, although the default template argument may be considered a bit unusual. I personally like the following style better, which is similar to how you (read: I) write a trait to check for a function's existence, just using a non-type template parameter and leaving out the decltype:

#include <type_traits>

namespace detail{
template<int> struct sfinae_true : std::true_type{};
template<class T>
sfinae_true<(T::f(), 0)> check(int);
template<class>
std::false_type check(...);
} // detail::

template<class T>
struct has_constexpr_f : decltype(detail::check<T>(0)){};

Live example.


Explanation time~

Your original code works? because a default template argument's point of instantiation is the point of instantiation of its function template, meaning, in your case, in main, so it can't be substituted earlier than that.

§14.6.4.1 [temp.point] p2

If a function template [...] is called in a way which uses the definition of a default argument of that function template [...], the point of instantiation of the default argument is the point of instantiation of the function template [...].

After that, it's just usual SFINAE rules.


? Atleast I think so, it's not entirely clear in the standard.

這篇關于用 SFINAE 檢測 constexpr的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(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++ - 如何從指向向量的指針訪問向量的內(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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 欧美一区二区三区在线观看 | 免费v片 | 久久久久久九九九九九九 | 久久久久国产精品一区二区 | 99久久精品免费看国产免费软件 | 女女百合av大片一区二区三区九县 | 一级二级三级在线观看 | 精品国产乱码久久久久久果冻传媒 | 免费成人高清在线视频 | 一区在线视频 | 国产一区二区三区四区在线观看 | 国产精品爱久久久久久久 | 国产精品成人一区二区三区吃奶 | 久久免费观看视频 | 91国自视频 | 中文字幕在线三区 | 日韩一区二区在线免费观看 | 在线精品观看 | 精品久久一区 | 色爱综合网 | 亚洲欧美中文字幕在线观看 | 日韩av一区二区在线观看 | 色播av| 男人亚洲天堂 | 亚洲成人精品 | 亚洲精品日韩综合观看成人91 | 亚洲福利一区二区 | 亚洲欧美视频一区二区 | 国产麻豆乱码精品一区二区三区 | 欧美成ee人免费视频 | 日日操夜夜操天天操 | 欧美极品少妇xxxxⅹ免费视频 | 伊人久久综合 | 看一级毛片视频 | 国产精品久久久久久久久久久久久 | 日韩一区二区在线看 | 日韩在线一区二区三区 | 亚洲三区在线观看 | 99精品在线 | 国产精品久久久久久久久久久久久久 | 亚洲视频免费播放 |