Michael Medvinsky
Michael Medvinsky

Reputation: 326

how to std::bind templated function?

1) I have function declared as

    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?

 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

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

Upvotes: 2

Views: 1336

Answers (1)

Oleg Bogdanov
Oleg Bogdanov

Reputation: 1732

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

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

n4618 14.1.11 says:

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).

cppreference alludes similarly:

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

Upvotes: 2

Related Questions