gpt4 book ai didi

c++ - 带有限定符的函数类型 typedef 的用例

转载 作者:行者123 更新时间:2023-12-02 03:46:30 25 4
gpt4 key购买 nike

即使使用类 cv 和 ref 限定符,也可以使用 typedef 函数类型:

typedef void F() const volatile &&;

这可用于声明(但不能定义)非静态成员函数:

struct A {
F f; // void f() const volatile &&
};

在模板的依赖上下文中明确禁止使用 F

它也可以用作模板参数。据我所知,没有办法提取或操作该类型的 cv 和 ref 限定符。

这样的 typedef 是否还有其他用例(使用任何限定符,不一定同时使用所有限定符)?

最佳答案

该标准在[dcl.fct]/6中指定了此类函数类型(带有cv-qualifier-seqref-qualifier的函数类型)的唯一允许用例。 :

A function type with a cv-qualifier-seq or a ref-qualifier (including a type named by typedef-name ([dcl.typedef], [temp.param])) shall appear only as:

  • (6.1) the function type for a non-static member function,

  • (6.2) the function type to which a pointer to member refers,

  • (6.3) the top-level function type of a function typedef declaration or alias-declaration,

  • (6.4) the type-id in the default argument of a type-parameter, or

  • (6.5) the type-id of a template-argument for a type-parameter ([temp.arg.type]).

最小可重现代表性示例(mre):

using Func = void() const;
using Func2 = Func; // (6.3)

struct C {
Func f; // (6.1)
};

Func C::* ptr; // (6.2)

template <class T = Func> // (6.4)
struct S { };

S<Func> x; // (6.5)

关于c++ - 带有限定符的函数类型 typedef 的用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58095191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com