Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

c++ - variadic templates parameter matching in std::function

I have the following code:

#include <iostream>
#include <functional>

template<typename Return, typename... Params>
void func(std::function<Return(Params... )> x) {}

void f(double) {}

int main() {
    //func<void, double>(f); // compile error here in the variadic case
    func(std::function<void(double)>(f));
}

I have 2 questions:

1. I do not understand why does the line func<void, double>(f); give me a compiling error

/Users/vlad/minimal.cpp:10:5: error: no matching function for call to 'func'
    func<void, double>(f); // compile error here in the variadic case
    ^~~~~~~~~~~~~~~~~~
/Users/vlad/minimal.cpp:5:6: note: candidate template ignored: could not match 'function<void (double, type-parameter-0-1...)>' against 'void (*)(double)'
void func(std::function<Return(Params... )> x) {}
     ^
1 error generated.

whereas if I cast the parameter f to a std::function (as in the non-commented line) it works.

2. And the most puzzling issue is that, if I use a non-variadic version of func (i.e. just replace typename... by typename so in effect func takes a std::function<Return(Params)> as parameter), then the commented line in main works as desired. Any ideas why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do not understand why does the line func<void, double>(f); give me a compiling error

The compiler doesn't know that you want Params to be exactly double, it thinks maybe you want it to deduce a pack with more elements, such as double, int, void*, char or double, double, double or some other pack of types, and it doesn't know how to deduce that from the argument f.

In theory there could be other specializations of std::function which could be constructible from f and which would allow the compiler to deduce a pack of more than one type for Params (it can't know that isn't true without instantiating every possible specialization of std::function and testing them, which is not feasible.

whereas if I cast the parameter f to a std::function (as in the non-commented line) it works.

Because now the compiler is able to deduce Params correctly.

And the most puzzling issue is that, if I use a non-variadic version of func [...] then the commented line in main works as desired. Any ideas why?

Because now the compiler knows that Params is a single type, not a pack of zero or more types, so when you say func<void, double> it knows Params is double, and not double, int, void*, char or some other parameter pack.

Edit in answer to your comment, consider this:

template<typename T, typename U, typename V>
int func(T t, U u, V v)
{ return 0; }

int i = func<int, char>(1, '2', "three");

I've only given an explicit template argument for two of the parameters, so the third must still be deduced.

When you have a variadic template there could be any number of other parameters remaining to be deduced.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...