LINUX.ORG.RU

как заставить работать перегрузку функций?

 


0

3
void f(int x) { std::cout << x << " f\n"; }
void f(int x, int y) { std::cout << x << y << " g\n"; }

struct  A{
	int i;
	std::function<void(int)> f;
};

int main() {
	A a;
	a.i = 1;
	a.f = f;
	a.f(a.i);
	return 0;
}
prog.cpp: In function ‘int main()’:
prog.cpp:15:8: error: no match for ‘operator=’ (operand types are ‘std::function<void(int)>’ and ‘<unresolved overloaded function type>’)
  a.f = f;
        ^

#include <iostream>
#include <functional>

void f(int x) { std::cout << x << " f\n"; }
void f(int x, int y) { std::cout << x << y << " g\n"; }

struct  A{
	int i;
	std::function<void(int)> f;
};

int main() {
	A a;
	a.i = 1;
	a.f = static_cast<void (*)(int)>(&f);
	a.f(a.i);
	return 0;
}
fsb4000 ★★★★★
()

не заморачивайся с этим, перепиши на rust и поменяй лицензию

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.