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

讓模板參數成為朋友?

Making a template parameter a friend?(讓模板參數成為朋友?)
本文介紹了讓模板參數成為朋友?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

示例:

template<class T>
class Base {
public:
    Base();
    friend class T;
};

現在這不起作用...有沒有辦法做到這一點?

Now this doesn't work... Is there a way of doing this?

我實際上正在嘗試制作一個像這樣的通用類密封器:

I'm actually trying to make a general class sealer like this:

class ClassSealer {
private:
   friend class Sealed;
   ClassSealer() {}
};
class Sealed : private virtual ClassSealer
{ 
   // ...
};
class FailsToDerive : public Sealed
{
   // Cannot be instantiated
};

我在這個網站的某個地方找到了這個例子,但我找不到它......(這里)

I found this example on this site somewhere but I can't find it... (here)

我知道有其他方法可以做到這一點,但剛才我很好奇你是否真的可以做這樣的事情.

I know there are other ways of doing this but just now I'm curious if you actually can do something like this.

推薦答案

它在標準中被明確禁止,即使某些版本的 VisualStudio 允許它.

It is explicitly disallowed in the standard, even if some versions of VisualStudio do allow it.

C++ 標準 7.1.5.3 詳細類型說明符,第 2 段

C++ Standard 7.1.5.3 Elaborated type specifiers, paragraph 2

3.4.4 描述了如何在一個標識符中進行名稱查找.詳細類型說明符.如果標識符解析為一個類名或枚舉名,詳細類型說明符引入它進入聲明相同簡單類型說明符引入的方式它的類型名稱.如果標識符解析到 typedef 名稱或模板類型參數,詳細類型說明符是畸形.[注意:這意味著,在一個類模板中模板類型參數 T,聲明朋友類T;是畸形.]

3.4.4 describes how name lookup proceeds for the identifier in an elaborated-type-specifier. If the identifier resolves to a class-name or enum-name, the elaborated-type-specifier introduces it into the declaration the same way a simple-type-specifier introduces its type-name. If the identifier resolves to a typedef-name or a template type-parameter, the elaborated-type-specifier is ill-formed. [Note: this implies that, within a class template with a template type-parameter T, the declaration friend class T; is ill-formed. ]

我認為上面的代碼是一種密封(禁止擴展)類的模式.還有另一種解決方案,它不會真正阻止擴展,但會標記無意中從類中擴展.正如在 ADOBE 源庫中所見:

I recognize the code above as a pattern to seal (disallow the extension of) a class. There is another solution, that does not really block the extension but that will flag unadvertidly extending from the class. As seen in ADOBE Source Library:

namespace adobe { namespace implementation {
template <class T>
class final
{
protected:
   final() {}
};
}}
#define ADOBE_FINAL( X ) private virtual adobe::implementation::final<T>

用法:

class Sealed : ADOBE_FINAL( Sealed )
{//...
};

雖然它允許擴展,如果你真的強迫它:

While it allows extension if you really force it:

class SealBreaker : public Sealed, ADOBE_FINAL( Sealed )
{
public:
   SealBreaker() : adobe::implementation::final<Sealed>(), Sealed() {}
};

它會限制用戶誤操作.

編輯:

即將到來的 C++11 標準確實允許您與語法略有不同的類型參數成為朋友:

The upcoming C++11 standard does allow you to befriend a type argument with a slightly different syntax:

template <typename T>
class A {
   // friend class T; // still incorrect: elaborate type specifier
   friend T;          // correct: simple specifier, note lack of "class"
};

這篇關于讓模板參數成為朋友?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 | 美女一级毛片 | 成人午夜黄色 | 欧美精品在线一区二区三区 | 999久久久| 国产欧美精品一区二区色综合朱莉 | 日韩精品中文字幕在线 | 日韩在线中文字幕 | 国产重口老太伦 | 99免费精品视频 | 国产一区精品在线 | 成人免费精品 | 日本一区二区三区在线观看 | 精品一区二区三区在线观看 | 国产成人精品久久 | 国产91精品久久久久久久网曝门 | 成人深夜福利 | 国产高清免费 | 亚洲精品99 | 亚洲精品国产成人 | 婷婷五月色综合 | 欧美精品在线播放 | 国产精品99久久久久久久久久久久 | 狠狠草视频 | 精精国产xxxx视频在线野外 | 国产1区2区在线观看 | 欧美精品一区二区三区蜜桃视频 | 99精品在线 | 曰批视频在线观看 | 亚洲精品久久久久久一区二区 | 欧美日韩国产在线 | 亚洲欧美日韩久久 | 伊人无码高清 | 国产在线视频一区二区董小宛性色 | 午夜成人在线视频 | 欧美一卡二卡在线 | 亚洲综合色视频在线观看 | 欧美一级久久 |