C ++ overload: string literal vs boost :: function ambiguity

my problem code is:

#include <string>
#include <boost/function.hpp>

void func (const std::string&) {}
void func (const boost::function<void()>&) {}

int main() {
    func (main); // good
    func ("bad");
    return 0;
}

=>

error: call of overloaded ‘func(const char [4])’ is ambiguous
overload.cpp:4: note: candidates are: void func(const std::string&)
overload.cpp:5: note:                 void func(const boost::function<void ()()>&)

I know that I can solve this by explicitly calling func (string ("bad")); or by creating func (const char *), but I wonder if there is a way to keep the caller's side, as in the example, and not introduce more overloads.

Maybe something with boost :: enable_if? Thanks for any tips.

+5
source share
2 answers

. boost::function<> std::function<> , f(), , (secondArg.*firstArg)() , , , ,

SFINAE, ( , , . ). , - - , .. SFINAE, , , .

, , .

+4

:

void func (const char *s) {  func(string(s)); }

template<class A0, ...>
void func (const A0 &a0, ...) {
    func(argize(a0), ...); // convert chars to strig, otherwise jut pass
}
0

All Articles