且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ref合格的成员函数作为模板参数?

更新时间:2022-06-27 02:40:44

根据8.3.5 [dcl.fct]第6段(我在引用的文本中添加了一些突出显示的内容):

According to 8.3.5 [dcl.fct] paragraph 6 (I added some highlightening to the quoted text):

返回类型,参数类型列表, ref限定符和cv-qualifier-seq,但不包含默认参数(8.3.6)或异常规范(15.4)),是函数类型的一部分.

The return type, the parameter-type-list, the ref-qualifier, and the cv-qualifier-seq, but not the default arguments (8.3.6) or the exception specification (15.4), are part of the function type.

也就是说,ref限定符肯定是该类型的一部分.进一步根据8.4.1 [dcl.fct.def.general]第5段,您可以创建指向成员的指针,包括ref限定词:

That is, the ref-qualifier is certainly part of the type. Further according to 8.4.1 [dcl.fct.def.general] paragraph 5 you can create pointer-to-members including the ref-qualifiers:

cv-qualifier-seq或 ref-qualifier (或二者兼有)可以是非静态成员函数声明,非静态成员函数定义的一部分,或仅指向成员函数的指针(8.3.5);参见9.3.2.

A cv-qualifier-seq or a ref-qualifier (or both) can be part of a non-static member function declaration, non-static member function definition, or pointer to member function only (8.3.5); see 9.3.2.

没有特殊限制,即带有ref-qualifier的成员函数的指针不能用作非类型模板参数.也就是说,我认为您尝试使用的部分专业化应该起作用.但是,在lang和gcc中,对ref限定符的支持是一个相当新的功能,即可能不是所有的极端情况都已解决.我在上面用gcc(20130811)和clang(trunk 190769)的最新快照尝试了上述片段,并且都编译了代码.当然,此代码片段实际上并没有做任何事情,我也没有尝试滥用此功能.我猜想您刚刚触发了一些编译器错误,并且我相信两个项目都将感谢使用最新快照生成的错误报告.

There is no specific restriction that pointer to member functions with ref-qualifier cannot be used as non-type template arguments. That is, I think the partial specialization you tried to use should work. However, support for ref-qualifiers is a fairly new feature in both clang and gcc, i.e., probably not all corner cases have been ironed out. I tried the snipped above with fairly recent snapshots of both gcc (20130811) and clang (trunk 190769) and both compiled the code OK. Of course, this snippet doesn't really do anything and I didn't try to abuse this feature. I would guess you just triggered a few compiler bugs and I'm sure that both projects would appreciate error reports against their latest snapshots.