#class with overloaded variadic operator()

7 messages · Page 1 of 1 (latest)

keen shale
#

hi, i have a problem with following code

#include <iostream>
#include <print>
#include <typeinfo>

template <typename T> std::string type_name();

template <typename... T>
void showNames(T... a) {
    ((std::cout 
      << typeid(a).name() << " "
      << " " << 
      a << std::endl
    ), ...);
}

double f(int x, double y, const int &z, int &w){
    w += 2;
    std::cout << x << " " << y << " " << z << " " << w << std::endl;
    return (x * y - z * w);
}

template <class F>
class Proxy {
public:
    F f;

    Proxy(F f) : f(f) {}
    
    auto operator()(auto&&... a) {
        return f(a...);
    }
};

int main(){
    int x = 4;
    const int y = 8;
    showNames(x, 4.5, y, f);
    showNames(1, 1.0f, 1.0, 1LL, &x, &y);

    //auto p = make_proxy(f);
    auto p = Proxy(f);    /// with C++ 17
    auto result1 = p(12, 5.1, y, x);
    std::cout << "result1 = " << result1 << std::endl;
    auto result2 = p(12, 5.1, y, x);
    std::cout << "result2 = " << result2 << std::endl;
    auto result3 = p(3, 3, 5, x);
    std::cout << "result3 = " << result3 << std::endl;
    //auto g = make_proxy([](int &&x, int & y){ y = x; return y; }) ;
    auto g = Proxy([](int &&x, int & y) { y = x; return y; }) ; // with C++ 17
    std::cout << g(5, x) << std::endl;
    std::cout << "x = " <<  x << std::endl;

    return 0;
}

I get a following error, how can i fix it?

woven charmBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

wet smelt
#

You forgot to std::forward a

fallow ermine
#
        return f(std::forward<Ts>(a)...);
```or
```c++
        return f(std::forward<decltype(a)>(a)...);
#

[](int &&x, int & y) { y = x; return y; } expects an rvalue, return f(a...); does not forward an rvalue.

keen shale
#

ohh, right, thank you ^^

#

!solved