相关疑难解决方法(0)

用于确定类型是否可调用的C++元函数

是否可以编写一个C++(0x)元函数来确定一个类型是否可调用?

通过可调用类型我的意思是函数类型,函数指针类型,函数引用类型(这些被检测到boost::function_types::is_callable_builtin),lambda类型,以及任何具有重载的类operator()(也许任何类具有隐式转换运算符到其中一个,但这不是绝对有必要).

编辑:元函数应检测operator()任何签名的存在,包括模板化operator().我相信这是困难的部分.

编辑:这是一个用例:

template <typename Predicate1, typename Predicate2>
struct and_predicate
{
    template <typename ArgT>
    bool operator()(const ArgT& arg)
    {
        return predicate1(arg) && predicate2(arg);
    }

    Predicate1 predicate1;
    Predicate2 predicate2;
};

template <typename Predicate1, typename Predicate2>
enable_if<ice_and<is_callable<Predicate1>::value,
                  is_callable<Predicate2>::value>::value,
          and_predicate<Predicate1, Predicate2>>::type
operator&&(Predicate1 predicate1, Predicate2 predicate2)
{
    return and_predicate<Predicate1, Predicate2>{predicate1, predicate2};
}
Run Code Online (Sandbox Code Playgroud)

is_callable 是我想要实现的.

c++ metaprogramming c++11

29
推荐指数
3
解决办法
8388
查看次数

如何为模板化运算符()编写尽可能最好的is_callable trait

我有像这样定义的is_callable trait:

#ifndef IS_CALLABLE_HPP
#define IS_CALLABLE_HPP

#include <type_traits>

namespace is_callable_detail
{
    struct no   {};
    struct yes  { no x[2]; };

    template<bool CallableArgs, typename Callable, typename ReturnType, typename ...Args>
    struct check_return
    {
        static const bool value = std::is_convertible<decltype(std::declval<Callable>()(std::declval<Args>()...)), ReturnType>::value;
    };

    template<typename Callable, typename ReturnType, typename ...Args>
    struct check_return<false, Callable, ReturnType, Args...>
    {
        static const bool value = false;
    };
}

template<typename Callable, typename Function>
struct is_callable;

template<typename Callable, typename ReturnType, typename ...Args>
struct is_callable<Callable, ReturnType(Args...)>
{
    private:
        template<typename T>
        static is_callable_detail::yes …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae c++11

11
推荐指数
1
解决办法
1727
查看次数

使用C++ 17检测仿函数的类型特征?

问题描述:

C++17介绍std::invocable<F, Args...>,很好地检测类型...是否可以使用给定的参数进行调用.但是,是否有办法为仿函数的任何参数执行此操作(因为标准库的现有特征的组合已经允许检测函数,函数指针,函数引用,成员函数......)?

换句话说,如何实现以下类型特征?

template <class F>
struct is_functor {
    static constexpr bool value = /*using F::operator() in derived class works*/;
};
Run Code Online (Sandbox Code Playgroud)

使用示例:

#include <iostream>
#include <type_traits>

struct class0 {
    void f();
    void g();
};

struct class1 {
    void f();
    void g();
    void operator()(int);
};

struct class2 {
    void operator()(int);
    void operator()(double);
    void operator()(double, double) const noexcept;
};

struct class3 {
    template <class... Args> constexpr int operator()(Args&&...);
    template <class... Args> constexpr int operator()(Args&&...) const;
};

union union0 …
Run Code Online (Sandbox Code Playgroud)

c++ functor sfinae template-meta-programming c++17

9
推荐指数
1
解决办法
460
查看次数

可以在C++ 11中模拟std :: is_invocable吗?

我想使用std :: is_invocable,但我们使用的是c ++ 11标准,而is_invocable只能从c ++ 17获得.

有没有办法使用c ++ 11模拟功能?

谢谢

c++ c++11 c++17

9
推荐指数
1
解决办法
1085
查看次数

检查一个类是否有一个具有给定名称但有任何签名的方法

我试图找到一种方法来简单地检查给定名称的方法是否存在于使用 c++11 特性的 c++ 类中,但没有(!)检查签名。

如果没有签名检查,我无法找到任何东西,所以我尝试从这里使用 Valentin Milea 的解决方案并对其进行修改(见下文),但我对 C++ 的理解不够深入,无法真正掌握那里发生的事情:

#include <type_traits>

template <class C>
class HasApproxEqualMethod
{
    template <class T>
    static std::true_type testSignature(bool (T::*)(const T&, double) const);

    template <class T>
    static decltype(testSignature(&T::approx_equal)) test(std::nullptr_t);

    template <class T>
    static std::false_type test(...);

public:
    using type = decltype(test<C>(nullptr));
    static const bool value = type::value;
};

class Base {
public:
virtual ~Base();
virtual bool approx_equal(const Base& other, double tolerance) const;
};

class Derived : public Base {
public:
     // same interface …
Run Code Online (Sandbox Code Playgroud)

c++ methods templates sfinae c++11

4
推荐指数
2
解决办法
1762
查看次数