簡體   English   中英

C::* 在 typename 參數中是什么意思

[英]What does C::* mean in typename argument

我知道這里有類似的問題

我的問題與此略有不同,這是什么意思當 c::* 出現在模板參數中時,

template <typename C>
static yes& f(typename std::enable_if<
    std::is_same<decltype(static_cast<typename C::const_iterator(C::*)() const>(&C::begin)),
    typename C::const_iterator(C::*)() const>::value>::type*);

這里

誰能解釋一下這段代碼是什么意思,尤其是這個

類型名稱 C::const_iterator(C::*)()

當 c::* 出現時是什么意思

在上下文之外,它是指向c成員的指針的語法的一部分。

When put in context: C::const_iterator(C::*)() const type is pointer to const qualified member function of C that returns C::const_iterator and has empty parameter list.

恕我直言,那里的陳述結構非常糟糕,不可讀。 但它可以分層“拆解”:

template <typename C>
static yes & f (
    typename std::enable_if<
        std::is_same<
            decltype(
                static_cast<
                     typename C::const_iterator(C::*)() const
                >(&C::begin)
            ),
            typename C::const_iterator(C::*)() const
        >::value
    >::type *
);

它聲明了一個帶有 class 參數 C 的 function f返回yes&的模板。

The function instance will have a parameter of type void* if typename C::const_iterator(C::*)() const and the type of result of cast of &C::begin to C::const_iterator(C::*)() const相同,否則由於 SFINAE,模板不會產生合法實例,因為std::enable_if<>::type不會被聲明(如果exprstd::enable_if<expr>::type默認為void真的)

typename C::const_iterator(C::*)() const is a pointer to member of class C with signature similar to C::const_iterator name () const , ie it returns C::const_iterator , takes no parameters, and is a常量成員。 在 C++20 之前的標准中,如果聲明依賴於模板參數,則必須使用typename ,似乎建議在最新標准中取消要求。

static_cast<typename C::const_iterator(C::*)() const>可能會在C::begin是一個重載的 ZC1C425268E68385D1AB5074 成員時保護它,C 會選擇正確的重載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM