Where exactly do function pointers point?
Tag : cpp , By : algoRhythm99
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Function pointer also point into memory, the only difference is that there is executable code at that memory location instead of data. On many platforms if you try to execute data (e.g. regular memory) you'll crash or cause an exception. This is known as Data Execution Prevention - a security measure to prevent applications inadvertently running dodgy code that may be placed there by malware.
|
Function pointers: is the simple canonical use bad from a performance point of view? If so what are the c++11-ish altern
Date : March 29 2020, 07:55 AM
will be helpful for those in need You'd have to profile of course, but generally speaking the performance difficulty with calling through function pointers is that the compiler can't give you the benefits of inlining. Something like this might be faster: enum FuncTodo{
PLUS,
MINUS,
};
int operation(int first, int second, FuncTodo todo) {
switch(todo) {
case PLUS: return first + second;
case MINUS: return first - second;
}
}
|
Is there a function in C++ that will get you the address space of all the pointers that point to that node?
Date : March 29 2020, 07:55 AM
Does that help Such function does not exist and could not exist in C++, since C++ does not have a virtual machine. This could be a part of a garbage collector, but C++ does not have one. Collecting this kind of information would contradict zero-overhead principle of the language.
|
Is it possible for C++ to implement function pointers point to different parameter lists?
Tag : cpp , By : Star Gryphon
Date : March 29 2020, 07:55 AM
may help you . You basically are asking to have fully dynamicly typed and checked function calls. To have fully dynamic function calls, you basically have to throw out the C++ function call system. using dynamic_function = std::function< std::any( std::vector<std::any> ) >
struct nothing_t {};
template<class R, class...Args, class F>
struct dynamic_function_maker {
template<std::size_t...Is>
dynamic_function operator()(std::index_sequence<Is...>, F&& f)const {
return [f=std::forward<F>(f)](std::vector<std::any> args)->std::any {
if (sizeof...(Is) != args.size())
throw std::invalid_argument("Wrong number of arguments");
if constexpr( std::is_same< std::invoke_result_t<F const&, Args... >, void >{} )
{
f( std::any_cast<Args>(args[Is])... );
return nothing_t{};
}
else
{
return f( std::any_cast<Args>(args[Is])... );
}
};
}
dynamic_function operator()(F&& f)const {
return (*this)(std::make_index_sequence<sizeof...(Args)>{}, std::forward<F>(f));
}
};
template<class R, class...Args, class F>
dynamic_function make_dynamic_function(F f){
return dynamic_function_maker<R,Args...,F>{}(std::forward<F>(f));
}
template<class R, class...Args>
dynamic_function make_dynamic_function(R(*f)(Args...)){
return dynamic_function_maker<R,Args...,F>{}(std::forward<F>(f));
}
template<class Tclass R, class...Args>
dynamic_function make_dynamic_function(T* t, R(T::*f)(Args...)){
return dynamic_function_maker<R,Args...,F>{}(
[t,f](auto&&...args)->decltype(auto){return (t->*f)(decltype(args)(args)...);}
);
}
|
Equality of function pointers which point to lambda function
Tag : cpp , By : user181345
Date : March 29 2020, 07:55 AM
will help you You are right that lambdas are unique, and this is exactly why the second assertion passes. But they are not always different on each evaluation: template <typename>
auto get() { return []{}; }
auto l1 = get<int>();
auto l2 = get<long>();
auto l3 = get<int>();
static_assert(std::is_same_v<decltype(l1), decltype(l2)>); // fails
static_assert(std::is_same_v<decltype(l1), decltype(l3)>); // passes
// caveat: needs C++20
l1 = l2; // no, different lambdas
l1 = l3; // ok, same lambda
|