如何std :: bind模板化函数?

时间:2022-03-18 20:24:59

1) I have function declared as

1)我将函数声明为

    template< unsigned ... N, typename ... T2>
    auto Foo(T2 ... args);

it works properly when it called for example as

当它被称为例如时,它正常工作

double a = Foo<1>(1.0);

but doesn't even compile as

但是甚至没有编译成

double a = Foo<1,double>(1.0);

Why it so?

为什么会这样?

2) The actual problem is that neither of the following compiles, so how to bind it correctly?

2)实际问题是以下都没有编译,所以如何正确绑定它?

 std::function<double(double)> f = std::bind(&Foo<1>,std::placeholders::_1); 
 std::function<double(double)> g = std::bind(&Foo<1,double>,std::placeholders::_1); 

Edit
Thanks to Jonathan Wakely 3) The reason to std::bind it comes from

编辑感谢Jonathan Wakely 3)std :: bind它的原因来自于

class Bar
{
    Bar(std::function<T(T)> &f);
};

1 个解决方案

#1


1  

I'm only trying to cover Why it so? part assuming you are getting smth like

我只想说明为什么会如此?部分假设你像smth一样

note: candidate template ignored: invalid explicitly-specified argument for 1st template parameter

注意:忽略候选模板:第一个模板参数的显式指定参数无效

n4618 14.1.11 says:

n4618 14.1.11说:

A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list (8.3.5) of the function template or has a default argument (14.8.2).

函数模板的模板参数包不能跟随另一个模板参数,除非该模板参数可以从函数模板的参数类型列表(8.3.5)中推导出来或者具有默认参数(14.8.2)。

cppreference alludes similarly:

cppreference类似地暗示:

In a function template, the template parameter pack may appear earlier in the list provided that all following parameters can be deduced from the function arguments

在函数模板中,模板参数包可以出现在列表的前面,前提是可以从函数参数中推导出所有后续参数

Your first parameter pack is not among function arguments so its impossible to deduce it properly

您的第一个参数包不在函数参数中,因此无法正确推导它

#1


1  

I'm only trying to cover Why it so? part assuming you are getting smth like

我只想说明为什么会如此?部分假设你像smth一样

note: candidate template ignored: invalid explicitly-specified argument for 1st template parameter

注意:忽略候选模板:第一个模板参数的显式指定参数无效

n4618 14.1.11 says:

n4618 14.1.11说:

A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list (8.3.5) of the function template or has a default argument (14.8.2).

函数模板的模板参数包不能跟随另一个模板参数,除非该模板参数可以从函数模板的参数类型列表(8.3.5)中推导出来或者具有默认参数(14.8.2)。

cppreference alludes similarly:

cppreference类似地暗示:

In a function template, the template parameter pack may appear earlier in the list provided that all following parameters can be deduced from the function arguments

在函数模板中,模板参数包可以出现在列表的前面,前提是可以从函数参数中推导出所有后续参数

Your first parameter pack is not among function arguments so its impossible to deduce it properly

您的第一个参数包不在函数参数中,因此无法正确推导它