从函数类型中删除所有限定符 [英] Stripping all qualifiers from a function type

查看:133
本文介绍了从函数类型中删除所有限定符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个可能具有 cv-qualifier-seq 和可能的 ref-qualifier 的varargs函数类型,可以写一个类型trait,

Given a possibly varargs function type with possibly a cv-qualifier-seq and possibly a ref-qualifier, is it possible to write a type trait that strips all the qualifiers without writing 4 * 3 * 2 = 24 partial specializations?

template<class T>
struct strip_function_qualifiers;

template<class R, class... Args>
struct strip_function_qualifiers<R(Args...)> { using type = R(Args...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args..., ...)> { using type = R(Args..., ...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args...) const> { using type = R(Args...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args..., ...) const > { using type = R(Args..., ...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args...) const &> { using type = R(Args...); };

template<class R, class... Args>
struct strip_function_qualifiers<R(Args..., ...) const & > { using type = R(Args..., ...); };

// etc. etc. for each possible combination (24 in total)

并使用新的事务性内存TS < a>向组合中添加 transaction_safe 这是否意味着我们需要为此编写48个部分特殊化?

And with the new transactional memory TS adding transaction_safe to the mix, does that mean we'll need to write 48 partial specializations for this?

编辑:引用这些奇怪函数类型的说明([dcl.fct] / p6,引用N4140):

Edit: Quoting the description of these weird function types ([dcl.fct]/p6, quoting N4140):


具有 cv-qualifier-seq 参照限定符的函数类型
(包括类型由typedef-name(7.1.3,14.1)命名)应出现
仅作为:

A function type with a cv-qualifier-seq or a ref-qualifier (including a type named by typedef-name (7.1.3, 14.1)) shall appear only as:


  • 非静态成员函数

  • 指向成员的指针所指向的函数类型,

  • 函数typedef声明的顶级函数类型或类型参数(14.1)的默认参数中的 别名声明,

  • (14.3.1)的模板参数类型参数中的

  • the function type for a non-static member function,
  • the function type to which a pointer to member refers,
  • the top-level function type of a function typedef declaration or alias-declaration,
  • the type-id in the default argument of a type-parameter (14.1), or
  • the type-id of a template-argument for a type-parameter (14.3.1).

[示例:

typedef int FIC(int) const;
FIC f; // ill-formed: does not declare a member function
struct S {
   FIC f; // OK
};
FIC S::*pm = &S::f; // OK

- 结束示例]

在函数
声明符中的 cv-qualifier-seq 效果不同于在
函数之上添加cv-qualification类型。在后一种情况下,会忽略 cv-qualifiers
[注意:具有 cv-qualifier-seq 的函数类型不是
cv限定类型;没有cv限定的函数类型。 - end
note
] [示例

The effect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of the function type. In the latter case, the cv-qualifiers are ignored. [Note: a function type that has a cv-qualifier-seq is not a cv-qualified type; there are no cv-qualified function types. — end note ] [ Example:

 typedef void F();
 struct S {
    const F f; // OK: equivalent to: void f();
 };

- 结束示例]

返回类型,参数类型列表
ref-qualifier 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. [Note: Function types are checked during the assignments and initializations of pointers to functions, references to functions, and pointers to member functions. — end note ]


推荐答案

- 定义一次并尽可能重复使用。

当限定符顶级时,可以避免这么大量的特殊化 - 我们可以使用 std :: remove_cv std :: remove_reference 在这种情况下,删除每个步骤中的所有正交限定符。不幸的是,这不适用于在您引用的段落中解释的功能: cv-qualifier是函数类型的一部分,而不是顶级。 void()const 是一个根本不同于 void()的类型,因此两者都必须匹配两个

I see no way around this - define it once and reuse it whenever possible.
Such a huge amount of specializations is avoidable when the qualifiers are top-level - we could use std::remove_cv or std::remove_reference in that case, removing all orthogonal qualifiers in each step. Unfortunately this is not applicable for functions as explained in the paragraph quoted by you: The e.g. cv-qualifier is part of the function type, and not top-level. void() const is a fundamentally different type than void(), and thus the both have to be matched by two different partial specializations.

您可以使用宏缩短所有专业化:

You can shorten all specializations using macros though:

#define REM_CTOR(...) __VA_ARGS__

#define SPEC(var, cv, ref) \
template <typename R, typename... Args> \
struct strip_function_qualifiers<R(Args... REM_CTOR var) cv ref > \
{using type = R(Args... REM_CTOR var);};

#define REF(var, cv) SPEC(var, cv,) SPEC(var, cv, &) SPEC(var, cv, &&)

#define CV(var) REF(var,) REF(var, const) \
                REF(var, volatile) REF(var, const volatile)

template <typename> struct strip_function_qualifiers;

CV(()) CV((,...))

演示

Boost。 PP也是可能的:

Demo.
Boost.PP is possible as well:

#include <boost/preprocessor/tuple/enum.hpp>
#include <boost/preprocessor/seq/elem.hpp>
#include <boost/preprocessor/seq/for_each_product.hpp>

#define REF  (&&)(&)()
#define CV   (const volatile)(const)(volatile)()
#define VAR  (())((,...)) // Had to add a comma here and use rem_ctor below,
                          // otherwise Clang complains about ambiguous ellipses

#define SPEC(r, product) \
template <typename R, typename... Args> \
struct strip_function_qualifiers<R(Args... BOOST_PP_TUPLE_ENUM( \
    BOOST_PP_SEQ_ELEM(0, product))) \
    BOOST_PP_SEQ_ELEM(1, product)   \
    BOOST_PP_SEQ_ELEM(2, product)>  \
{using type = R(Args... BOOST_PP_TUPLE_ENUM(BOOST_PP_SEQ_ELEM(0, product)));};

template <typename> struct strip_function_qualifiers;

BOOST_PP_SEQ_FOR_EACH_PRODUCT(SPEC, (VAR)(CV)(REF))

演示 。当添加新的限定符,例如 transaction_safe transaction_safe_noinherit 时,两种方法都不会更长。

Demo. Both methods won't get much longer when adding new qualifiers such as transaction_safe or transaction_safe_noinherit.

这里是一个修改的 SPEC ,它也定义了某些trait成员。

Here is a modified SPEC that also defines certain trait members.

#include <type_traits>

#include <boost/preprocessor/tuple/size.hpp>

// […]

#define SPEC(r, product)                                         \
template <typename R, typename... Args>                          \
struct strip_function_qualifiers<R(Args... BOOST_PP_TUPLE_ENUM(  \
    BOOST_PP_SEQ_ELEM(0, product))) \
    BOOST_PP_SEQ_ELEM(1, product)   \
    BOOST_PP_SEQ_ELEM(2, product)>  \
{                                     \
    using type = R(Args... BOOST_PP_TUPLE_ENUM(BOOST_PP_SEQ_ELEM(0, product))); \
                                                            \
private:                                                    \
    using cv_type = int BOOST_PP_SEQ_ELEM(1, product);      \
    using ref_type = int BOOST_PP_SEQ_ELEM(2, product);     \
public:                                                     \
    using is_const    = std::is_const<cv_type>;             \
    using is_volatile = std::is_volatile<cv_type>;          \
    using is_ref_qualified = std::is_reference<ref_type>;               \
    using is_lvalue_ref_qualified = std::is_lvalue_reference<ref_type>; \
    using is_rvalue_ref_qualified = std::is_rvalue_reference<ref_type>; \
    using is_variadic = std::integral_constant<bool,                          \
                       !!BOOST_PP_TUPLE_SIZE(BOOST_PP_SEQ_ELEM(0, product))>; \
};

这篇关于从函数类型中删除所有限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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