类似函数签名的表达式作为 C++ 模板参数

2021-12-13 00:00:00 templates c++

我正在查看 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)).

相关文章