c++ - Function pointer in Template -
in old code seeing function pointer used in template:
typedef int (*get_max)(); template<get_max func> get_max func() { return func; } as new template, lost , trying google more same theories how possible. link of book described? in advance
global functions have known addresses of known sizes, can used in compile-time expression, e.g. template parameters. on standard x86 platforms address has 32 bits, on x64 - 64 bits (that's why storing shouldn't use int, has 32 bits on both platforms, rather intptr_t).
so code doing returning pointer function func function has been specialised for. internally, returns address of passed function. may have confused function names decay function pointers (i.e. don't have use & function's address)
you can create template parameter being address of global non-static variable, if in these kind of things. helps understand what's going on in original example.
#include <iostream> #include <string> template<int* p> int* useless_proxy() { return p; } int foo = 666; int main() { std::cout << *useless_proxy<&foo>() << std::endl; // prints 666 };
Comments
Post a Comment