简体   繁体   English

Functor到可变参数模板功能

[英]Functor to variadic template function

I want to make functor to generic function, but I get compiler error. 我想把仿函数变成functor,但是我得到了编译错误。 Here is the code: 这是代码:

template <class T>
struct Creator
{
    template <typename...Ts>
    static std::shared_ptr<T> create(Ts&&... vs)
    {
        std::shared_ptr<T> t(new T(std::forward<Ts>(vs)...));
        return t;
    }
};

class Car:
        public Creator<Car>
{
    private:
        friend class Creator<Car>;
        Car()
        {
        }
};

int main()
{
    auto car=Car::create();
    std::function< std::shared_ptr<Car> () > createFn=&Car::create;

    return 0;
}

I get the following error in GCC 4.6.3 on the second statement(the first is OK): 我在第二个语句的GCC 4.6.3中得到以下错误(第一个是OK):

error: conversion from ‘<unresolved overloaded function type>’
       to non-scalar type ‘std::function<std::shared_ptr<Car>()>’ requested

Any hint appreciated. 任何暗示赞赏。

If the pointer of a template function is needed, the template must be instantiated first. 如果需要模板函数的指针,则必须首先实例化模板。

std::function<std::shared_ptr<Car>()> createFn = &Car::create<>;

This will make it compile on clang++ 3.1, but g++ 4.8 still refuses to compile, which I believe is a bug. 这将使它在clang ++ 3.1上编译,但g ++ 4.8仍然拒绝编译,我认为这是一个bug。

You could provide a lambda function instead: 您可以提供lambda函数:

std::function<std::shared_ptr<Car>()> createFn = []{ return Car::create(); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM