Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not use target<> to check if two std::function are equal #49

Open
czs108 opened this issue May 8, 2019 · 3 comments
Open

Can not use target<> to check if two std::function are equal #49

czs108 opened this issue May 8, 2019 · 3 comments
Labels

Comments

@czs108
Copy link

czs108 commented May 8, 2019

void Thread::StepInto(const StepCallback & cbStep)
{
StepInto();
auto target = cbStep.target<void()>();
for (const auto & cb : stepCallbacks)
{
if (target == cb.target<void()>())
{
puts("duplicate StepInto callback detected!");
return;
}
}
stepCallbacks.push_back(cbStep);
}

I run a test with following code, taget<void()> always return nullptr. So I can't use this to check if two std::function are equal.

#include <vector>
#include <functional>

typedef std::function<void()> StepCallback;

std::vector<StepCallback> stepCallbacks;

void StepInto(const StepCallback& cbStep)
{
	auto target = cbStep.target<void()>();
	for (const auto& cb : stepCallbacks)
	{
                // always get null
		if (target == cb.target<void()>())
		{
			puts("duplicate StepInto callback detected!");
			return;
		}
	}
	stepCallbacks.push_back(cbStep);
}

void fun1() {}

void fun2() {}

int main()
{
	StepInto(fun1);
	StepInto(fun1);
	StepInto(fun2);
	StepInto([]() {});
	StepInto(std::bind([]() {}));
	return 0;
}

output:

duplicate StepInto callback detected!
duplicate StepInto callback detected!
duplicate StepInto callback detected!
duplicate StepInto callback detected!

According to cppreference, the template type of target<> seems very strict. Or there is something wrong with my test?

@mrexodia
Copy link
Member

mrexodia commented May 8, 2019 via email

@czs108
Copy link
Author

czs108 commented May 8, 2019

But if target<> always return nullptr, how can if (target == cb.target<void()>()) check if I push_back the same callback function twice? It is if (nullptr == nullptr) actually.

@mrexodia
Copy link
Member

mrexodia commented May 8, 2019

Probably when this was written for Visual Studio 2013 the standard wasn't followed and everything was working... If you know some other way to check if two std::functions call the same function it would be appreciated.

@mrexodia mrexodia added the bug label Nov 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants