SmallChess
SmallChess

Reputation: 8106

Compiler error: failed to bind overloaded function with two arguments

I can't find a way to make my following code compile. I'm looking for a way to bind the first overloaded sn() which takes an enum, such that I can just pass a string to it. I don't need to bind the second overloaded.

#include <iostream>

struct Stats
{
    enum AlignMetrics
    {
        AlignExon,
        AlignIntron,
        AlignBase
    };

    // How to bind this function??
    inline double sn(const std::string &cID, enum AlignMetrics m) const
    {
        return 0.0;
    }

    inline double sn(const std::string &cID, const std::string &id) const
    {
        return 0.0;
    }
};


int main(int argc, const char * argv[])
{
    Stats stats;

    // Ok (calling directly)        
    stats.sn("", Stats::AlignMetrics::AlignExon);

    // Can't compile (I want to bind it such that it takes only the first argument)
    std::bind(static_cast<double (Stats::*)(const std::string &, enum Stats::AlignMetrics)>(&Stats::sn), &stats, std::placeholders::_1, Stats::AlignMetrics::AlignExon);

    return 0;
}

Upvotes: 1

Views: 45

Answers (2)

R Sahu
R Sahu

Reputation: 206557

You are missing a const:

std::bind(static_cast<double (Stats::*)(const std::string &, enum Stats::AlignMetrics) const>(&Stats::sn), &stats, std::placeholders::_1, Stats::AlignMetrics::AlignExon);
                                                                                     // ^^^ Missing

Suggestion for simplification:

double (Stats::*fptr)(const std::string &, enum Stats::AlignMetrics) const = &Stats::sn;
std::bind(fptr, &stats, std::placeholders::_1, Stats::AlignMetrics::AlignExon);

Upvotes: 2

M.M
M.M

Reputation: 141534

You can use a lambda:

auto func = [&](std::string const &s) { return stats.sn(s, Stats::AlignMetrics::AlignExon); };

Upvotes: 1

Related Questions