相关疑难解决方法(0)

如何获取`std :: function`的参数个数?

这有可能获得参数的数量std::function吗?有点像NumOfArgument<...>::value.

例如,NumOfArgument<function<int(int, int)> >::value应该是2.

c++ function c++11

15
推荐指数
1
解决办法
3945
查看次数

C++函数调用包装器,函数作为模板参数

我正在尝试创建一个通用的包装器函数,它将函数作为模板参数,并使用与该函数相同的参数作为其参数.例如:

template <typename F, F func>
/* return type of F */ wrapper(Ts... Args /* not sure how to get Ts*/)
{
    // do stuff
    auto ret = F(std::forward<Ts>(args)...);
    // do some other stuff
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

解决方案需要可以转换为具有相同类型的函数指针,func以便我可以将其传递给C api.换句话说,解决方案需要是一个函数而不是一个函数对象.最重要的是,我需要能够在包装函数中完成工作.

如果内联评论不清楚,我希望能够做如下的事情:

struct c_api_interface {
    int (*func_a)(int, int);
    int (*func_b)(char, char, char);
};

int foo(int a, int b)
{
    return a + b;
}

int bar(char a, char b, char c)
{
    return a + b * c; …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-functions variadic-templates

9
推荐指数
2
解决办法
3974
查看次数

搜索技巧以避免在类模板中创建字段

我有像类一样的范围保护(这是简化的测试用例):

template<void(*close)()>
struct Guard1
{
  template<typename O>
  Guard1(O open) { open(); }

  ~Guard1() { close(); }
};

void close() { std::cout << "close g1\n"; }

int main()
{
  Guard1<close> g1 = [](){ std::cout << "open g1\n"; };
}
Run Code Online (Sandbox Code Playgroud)

我修改它,使得close表达式也可以作为lambda给出:

class Guard2
{
  std::function<void()> close;
public:

  template<typename O, typename C>
  Guard2(O open, C close) : close(close)
  {
    open();
  }

  ~Guard2() { close(); }
};


int main()
{
  Guard2 g2(
      [](){ std::cout << "open g2\n"; },
      [](){ std::cout << "close g2\n"; }); …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
149
查看次数

具有任何参数列表的函数的泛型函子

我需要实现一个函数,它在实例化时接受任何(!)函数指针,分析参数类型,存储指针以及何时调用operator(),使用指针执行某些操作.最简单的情况是,用它的参数调用函数.

我尝试将函数指针转换为类似std :: function的东西,我得到错误:

error: invalid use of incomplete type ‘struct std::function<void (*)(...)>’
/usr/include/c++/4.6/functional:1572:11: error: declaration of ‘struct std::function<void(*)(...)>’
Run Code Online (Sandbox Code Playgroud)

我正在使用gcc 4.6.1,使用-std = c ++ 0x进行编译

这是一个最小的例子:

#include <functional>

using namespace std;
typedef void (*F_vararg)(...);
class Foo
{
    public:
        template<typename... ARGS> Foo(function<F_vararg(ARGS... args)> f);
        ~Foo(){}
        template<typename... ARGS>void operator()(ARGS... args);
    private:
        F_vararg m_f;
};

template<typename... ARGS> 
Foo::Foo(function<F_vararg(ARGS... args)> f)
{
    m_f = f;
}

template<typename... ARGS>
void Foo::operator()(ARGS... args)
{
    m_f(args...);
}

void func1(double *a1, double *a2, double *b)
{    //do something …
Run Code Online (Sandbox Code Playgroud)

generics variadic-functions functor variadic-templates c++11

3
推荐指数
1
解决办法
6220
查看次数

使用非const表达式作为模板参数

这是一个后续如何在可变参数模板类中获取函数指针的参数类型?

我有这个结构来访问可变参数模板的参数:

template<typename T> 
struct function_traits;  

template<typename R, typename ...Args> 
struct function_traits<std::function<R(Args...)>>
{
    static const size_t nargs = sizeof...(Args);

    typedef R result_type;

    template <size_t i>
    struct arg
    {
        typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
    };
};
Run Code Online (Sandbox Code Playgroud)

我访问了Args的参数类型

typedef function<void(Args...)> fun;
std::cout << std::is_same<int, typename function_traits<fun>::template arg<0>::type>::value << std::endl;
Run Code Online (Sandbox Code Playgroud)

但是,我想遍历参数以便能够处理任意数量的参数.以下不起作用,但说明我想要的:

for (int i = 0; i < typename function_traits<fun>::nargs ; i++){ 
    std::cout << std::is_same<int, typename function_traits<fun>::template arg<i>::type>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers functor variadic-templates c++11

3
推荐指数
1
解决办法
1278
查看次数

如何从实际函数中推导出std :: function参数?

上一堂课

class Foo {
 public:
  std::shared_ptr<const Bar> quux(const std::string&, std::uint32_t);
}
Run Code Online (Sandbox Code Playgroud)

我可以声明一个std::function具有相同接口的:

std::function<std::shared_ptr<const Bar>(const std::string&, std::uint32_t)> baz = ...
Run Code Online (Sandbox Code Playgroud)

有没有一种压缩该声明的方法,使得to的模板参数std::function是从该方法的声明派生的,例如:

std::function<functype(X::quux)> baz = ...
Run Code Online (Sandbox Code Playgroud)

其中functype是虚C ++操作类似decltype。有没有办法做到这一点/ 是否具有这种能力?

我确实看到该方法的签名实际上稍有不同,因为它也需要对该this对象的引用/指针。我也可以派出这样的签名。

c++ templates types c++11 std-function

3
推荐指数
1
解决办法
101
查看次数

我可以从(旧式)函数签名中获取参数包吗?

我想声明一个方法 - 可变参数 - 它的签名来自作为模板参数的“旧式”函数签名。

您可以std::function使用函数签名声明,例如,

std::function<int(float,float)> f;
Run Code Online (Sandbox Code Playgroud)

现在我想要一个模板,它采用这样的函数签名并以某种方式声明一个方法:

template <typename F>
struct Foo {
   [F's-return-type] Method(F's-Arg-pack]...) { ... }
};
Run Code Online (Sandbox Code Playgroud)

因此,如果您按如下方式实例化它,您将获得如下方法:

   Foo<int(float,float)> foo;
   int x = foo.Method(1.0f, 2.0f);
Run Code Online (Sandbox Code Playgroud)

或者也许有不同的方法来做到这一点?

c++ variadic-functions variadic-templates c++17

0
推荐指数
1
解决办法
58
查看次数