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

類似函數簽名的表達式作為 C++ 模板參數

Function signature-like expressions as C++ template arguments(類似函數簽名的表達式作為 C++ 模板參數)
本文介紹了類似函數簽名的表達式作為 C++ 模板參數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在查看 Don Clugston 的 FastDelegate 迷你庫,并注意到一個奇怪的語法技巧,如下所示結構:

I was looking at Don Clugston's FastDelegate mini-library and noticed a weird syntactical trick with the following structure:

TemplateClass< void( int, int ) > Object;

看起來好像函數簽名被用作模板實例聲明的參數.

It almost appears as if a function signature is being used as an argument to a template instance declaration.

這種技術(其出現在 FastDelegate 中顯然是由于一個 Jody Hagins)被用來簡化模板實例的聲明,模板參數的數量是半任意的.

This technique (whose presence in FastDelegate is apparently due to one Jody Hagins) was used to simplify the declaration of template instances with a semi-arbitrary number of template parameters.

也就是說,它允許這樣的事情:

To wit, it allowed this something like the following:

// A template with one parameter
template<typename _T1>
struct Object1
{
    _T1 m_member1;
};

// A template with two parameters
template<typename _T1, typename _T2>
struct Object2
{
    _T1 m_member1;
    _T2 m_member2;
};

// A forward declaration
template<typename _Signature>
struct Object;

// Some derived types using "function signature"-style template parameters
template<typename _Dummy, typename _T1>
struct Object<_Dummy(_T1)> : public Object1<_T1> {};

template<typename _Dummy, typename _T1, typename _T2>
struct Object<_Dummy(_T1, _T2)> : public Object2<_T1, _T2> {};

// A. "Vanilla" object declarations
Object1<int> IntObjectA;
Object2<int, char> IntCharObjectA;

// B. Nifty, but equivalent, object declarations
typedef void UnusedType;
Object< UnusedType(int) > IntObjectB;
Object< UnusedType(int, char) > IntCharObjectB;

// C. Even niftier, and still equivalent, object declarations
#define DeclareObject( ... ) Object< UnusedType( __VA_ARGS__ ) >
DeclareObject( int ) IntObjectC;
DeclareObject( int, char ) IntCharObjectC;

盡管有一點黑客的味道,但我發現這種對可變參數模板參數的欺騙性模擬非常令人興奮.

Despite the real whiff of hackiness, I find this kind of spoofy emulation of variadic template arguments to be pretty mind-blowing.

這個技巧的真正核心似乎是我可以將像Type1(Type2, Type3)"這樣的文本結構作為參數傳遞給模板.所以這里是我的問題:編譯器究竟是如何解釋這個構造的?是函數簽名嗎?或者,它只是一個帶有括號的文本模式?如果是前者,那么這是否意味著就模板處理器而言,任何任意函數簽名都是有效類型?

The real meat of this trick seems to be the fact that I can pass textual constructs like "Type1(Type2, Type3)" as arguments to templates. So here are my questions: How exactly does the compiler interpret this construct? Is it a function signature? Or, is it just a text pattern with parentheses in it? If the former, then does this imply that any arbitrary function signature is a valid type as far as the template processor is concerned?

一個后續的問題是,既然上面的代碼示例是有效的代碼,為什么 C++ 標準不只允許你做類似下面這樣的事情,這不會編譯?

A follow-up question would be that since the above code sample is valid code, why doesn't the C++ standard just allow you to do something like the following, which does not compile?

template<typename _T1>
struct Object
{
    _T1 m_member1;
};

// Note the class identifier is also "Object"
template<typename _T1, typename _T2>
struct Object
{
    _T1 m_member1;
    _T2 m_member2;
};

Object<int> IntObject;
Object<int, char> IntCharObject;

推薦答案

關于你的第一個問題 - 關于類型 int(char, float) - 這是一個有效的 C++ 類型,是接受 charfloat 并返回 int 的函數類型.請注意,這是實際函數的類型,而不是函數指針,它應該是 int (*) (char, float).任何函數的實際類型都是這種不尋常的類型.例如

With regards to your first question - about the type int(char, float) - this is a valid C++ type and is the type of a function that takes in a char and a float and returns an int. Note that this is the type of the actual function, not a function pointer, which would be an int (*) (char, float). The actual type of any function is this unusual type. For example, the type of

void DoSomething() {
    /* ... */
}

void().

在常規編程中很少出現這種情況的原因是在大多數情況下您不能聲明這種類型的變量.例如,此代碼是非法的:

The reason that this doesn't come up much during routine programming is that in most circumstances you can't declare variables of this type. For example, this code is illegal:

void MyFunction() { 
    void function() = DoSomething; // Error!
}

但是,您確實看到使用的函數類型的一種情況是傳遞函數指針:

However, one case where you do actually see function types used is for passing function pointers around:

void MyFunction(void FunctionArgument()) {
     /* ... */
}

看到這類函數被編寫成接收函數指針的情況更為常見,但接收函數本身也完全沒問題.它是在幕后鑄造的.

It's more common to see this sort of function written to take in a function pointer, but it's perfectly fine to take in the function itself. It gets casted behind-the-scenes.

至于你的第二個問題,為什么用不同數量的參數編寫相同的模板是非法的,我不知道規范中禁止它的確切措辭,但這與這樣一個事實有關你已經聲明了一個類模板,你不能改變它的參數數量.但是,您可以對具有不同數量參數的模板提供部分特化,當然前提是部分特化只特化原始數量的參數.例如:

As for your second question, why it's illegal to have the same template written with different numbers of arguments, I don't know the exactly wording in the spec that prohibits it, but it has something to do with the fact that once you've declared a class template, you can't change the number of arguments to it. However, you can provide a partial specialization over that template that has a different number of arguments, provided of course that the partial specialization only specializes over the original number of arguments. For example:

template <typename T> class Function;
template <typename Arg, typename Ret> class Function<Ret (Arg)> { 
    /* ... */
};

這里,Function 總是接受一個參數.模板特化接受兩個參數,但特化仍然只有一種類型(具體來說,Ret (Arg)).

Here, Function always takes one parameter. The template specialization takes in two arguments, but the specialization is still only over one type (specifically, Ret (Arg)).

這篇關于類似函數簽名的表達式作為 C++ 模板參數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 性欧美hd| 久久99国产精一区二区三区 | 一级免费毛片 | 亚洲成人精品视频 | 亚洲午夜精品一区二区三区他趣 | 亚洲欧美视频 | 理论片午午伦夜理片影院 | 精品一区二区三区在线视频 | 国产精品精品视频一区二区三区 | 久久91精品久久久久久9鸭 | 天天干人人 | 日韩在线观看网站 | 国产精品成人69xxx免费视频 | 亚洲成人黄色 | 99精品国产一区二区青青牛奶 | 91精品在线播放 | 欧美一区二区三区在线观看 | 亚洲 成人 av| 日韩美香港a一级毛片免费 国产综合av | 国产成人av在线播放 | 成年人国产在线观看 | www.99热.com| 日韩福利在线观看 | 亚洲淫视频 | 久久新| 天天影视亚洲综合网 | 亚洲国产成人av好男人在线观看 | 欧美人妖网站 | 久久综合伊人 | 中文字幕一区二区三区在线观看 | 一级二级三级黄色 | 亚洲精品在线视频 | 亚洲国产精品久久久久秋霞不卡 | 中文在线www| 国产乱码精品一区二区三区五月婷 | 久久久精品 | 欧美aaaaaaaaaa| 狠狠亚洲 | 亚洲一区二区三区在线播放 | 国产精品污www一区二区三区 | 欧美精品一区在线 |