ref合格的成员函数作为模板参数? [英] ref-qualified member functions as template arguments?

查看:46
本文介绍了ref合格的成员函数作为模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在clang 3.3中可以正常编译:

This compiles fine in clang 3.3:

template <typename T>
struct M;

template <typename R, typename C, typename... A>
struct M <R (C::*)(A...)> { };

template <typename R, typename C, typename... A>
struct M <R (C::*)(A...) &> { };

但在gcc 4.8.1中失败:

but fails in gcc 4.8.1:

[...] error: redefinition of ‘struct M <R (C::*)(A ...)>’
 struct M <R (C::*)(A...) &> { };
        ^
[...] error: previous definition of ‘struct M <R (C::*)(A ...)>’
 struct M <R (C::*)(A...)> { };
        ^

在不同的上下文中使用时,这会导致各种意外的编译器行为,例如崩溃或内部编译器错误.

When used in different contexts, this results in all sorts of unexpected compiler behaviour like crashing or internal compiler errors.

我了解ref合格的成员函数被称为"* this的右值引用"(N2439 ),并且由gcc 4.8.1支持

I understand ref-qualified member functions are referred to as "rvalue references for *this" (N2439) in the standard, and are supported by gcc 4.8.1.

这里的问题是将它们用作模板参数,其中gcc似乎无法区分ref限定类型和普通成员函数类型.

The problem here is to use them as template arguments, where gcc does not appear to discriminate between a ref-qualified and an ordinary member function type.

clang的std库实现似乎检测到该功能是否受

clang's implementation of the std library seems to detect whether this feature is supported by

__has_feature(cxx_reference_qualified_functions)

那么,这是使用标准的ref限定功能还是语言扩展?

So, is this use of ref-qualified functions standard, or a language extension?

推荐答案

根据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.

这篇关于ref合格的成员函数作为模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆