简体   繁体   English

检测模板化operator()的限定词

[英]Detect qualifiers of templated operator()

I'm looking for a trait to detect and to extract the full signature (to check for method qualifiers volatile and const ) of a templated operator() . 我正在寻找一个特征来检测和提取模板化operator()的完整签名(以检查方法限定符volatileconst operator()

Which means std::bind expressions (without the use of std::is_bind_expression ) and lambdas with auto parameters. 这意味着带有自动参数的std::bind表达式(不使用std::is_bind_expression )和lambda。 The expected return type and arguments are known . 预期的返回类型和参数是已知的

For example something like: 例如类似:

template<typename Fn, typename T>
struct is_templated_functor { ... };

auto fun = [](auto) mutable { };
using ty = decltype(&decltype(fun)::operator()<int>);
// ty = void(decltype(fun)::*)(int)
// lambda is mutable ~~~~~~~~~~~~~~~^^^^^^
is_templated_functor<void(int), decltype(fun)>::value == true;

auto fun2 = std::bind([](int) { });
using ty2 = decltype(&decltype(fun2)::operator()<int>);
// ty2 = void(decltype(fun2)::*)(int) const
is_templated_functor<void(int), decltype(fun2)>::value == true;

Due to @Yakk's comment i realized that i don't need to get the full signature of the expression to check if the expression is callable with a const and/or volatile qualifier, since the return type and arguments are known already. 由于@Yakk的评论,我意识到我不需要获取表达式的完整签名来检查表达式是否可以使用const和/或volatile限定符进行调用,因为返回类型和参数是已知的。

Its possible to use a detection idiome to check if the object with a templated operator() is callable with the given quaifiers: 可以使用检测惯用语来检查具有模板化operator()的对象是否可通过给定的量化器调用:

template<typename Fn>
struct impl_is_callable_with_qualifiers;

template<typename ReturnType, typename... Args>
struct impl_is_callable_with_qualifiers<ReturnType(Args...)>
{
    template<typename T>
    static auto test(int)
        -> typename std::is_convertible<
            decltype(std::declval<T&>()(std::declval<Args>()...)),
            ReturnType
           >;

    template<typename T>
    static auto test(...)
        -> std::false_type;
};

template<bool Condition, typename T>
using add_const_if_t = typename std::conditional<
    Condition,
    typename std::add_const<T>::type,
    T
>::type;

template<bool Condition, typename T>
using add_volatile_if_t = typename std::conditional<
    Condition,
    typename std::add_volatile<T>::type,
    T
>::type;

template<typename T, typename Fn, bool Constant, bool Volatile>
using is_callable_with_qualifiers = decltype(impl_is_callable_with_qualifiers<Fn>::template test<
    add_volatile_if_t<Volatile, add_const_if_t<Constant, typename std::decay<T>::type>>
>(0));

Usage example: 用法示例:

struct callable
{
    void huhu(int) const { }
};

auto fun = [](auto) mutable { };
static_assert(is_callable_with_qualifiers<decltype(fun), void(int), false, false>::value, "1 failed");
static_assert(!is_callable_with_qualifiers<decltype(fun), void(int), true, true>::value, "2 failed");

auto fun2 = std::bind(&callable::huhu, callable{}, std::placeholders::_1);

// std::bind isn't const correct anyway...
static_assert(is_callable_with_qualifiers<decltype(fun2), void(int), false, false>::value, "3 failed");
static_assert(is_callable_with_qualifiers<decltype(fun2), void(int), true, false>::value, "4 failed");
static_assert(!is_callable_with_qualifiers<decltype(fun2), void(int), true, true>::value, "5 failed");

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM