問題描述
我正在查看 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++ 類型,是接受 char
和 float
并返回 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模板網!