jeudi 30 juin 2016

term doesnt evaluate to 1 argument in std algorithm when using unary op defined with std::unique_ptr

I am trying to use a std::unique_ptr functor in std algorithm but if i use it like -

std::unique_ptr<IFormatter> format(new formatter("abcd"));
std::transform(vec.begin(), vec.end(), vec.begin(), format.get()) 

i am getting compilation error saying - "Error 1 error C2064: term does not evaluate to a function taking 1 arguments."

    #include <iostream>
    #include <string>
    #include<algorithm>
    #include<vector>
    using namespace std;


    struct IFormatter
    {
    protected:
    std::string keyFormat;
    void fun(std::string& str)
    {
        // do something here.
    }
public:
    IFormatter(std::string keyFormat) : keyFormat(keyFormat)
    {}
    virtual std::string operator() (const std::string &key) = 0;
};
struct formatter : IFormatter
{
    formatter(std::string keyFormat) : IFormatter(keyFormat)
    {}
    std::string operator() (const std::string &key)
    {
            std::string s = keyFormat;
            fun(s);
            return s;
    }
};

int main()
{        
    std::vector<std::string> vec{ "one", "two" };
    std::unique_ptr<IFormatter> format(new formatter("TRADE:INTRADAY:$ID$"));
    // This line compiles fine if i define format as - formatter format("abcd");
    std::transform(vec.begin(), vec.end(), vec.begin(), format.get());
    start s;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire