Tom
Tom

Reputation: 1291

C++ how to use std::function with overloaded operators of different return types

I'm trying to use the standard std::function with custom overloaded operators. However applying std::logical_and to my Test class with a string parameter is not working in this situation.

class Test {
public:
    std::string value;

    Test(std::string cvalue) : value(cvalue) {}

    std::string operator&& (const std::string& rhs) const {
        if (rhs == value) {
            return "true";
        }
        return "false";
    }
};

int main() {
    Test test("hey");
    std::string out = std::logical_and<std::string>()(test, "hey");
    std::cout << out << std::endl;
    return 0;
}

What is the proper way of achieving this? My expected output is "true"

Upvotes: 0

Views: 64

Answers (1)

Jarod42
Jarod42

Reputation: 217275

You need to use std::logical_and<> to allow deduction for both arguments and return type:

std::string out = std::logical_and<>()(test, "hey");

Demo

Upvotes: 5

Related Questions