c++ - Function using alias -
c++ - Function using alias -
what next code declare;
using f1 = void(int); i know following;
using f2 = void(*)(int); using f3 = void(&)(int); f2 pointer function , f3 reference.
what it?
it's function type. when declare function, such as:
void func(int); its type not pointer nor reference. above function's type void(int).
we can "prove" using type traits follows:
void func(int) {} int main() { std::cout << std::is_same<decltype(func), void(int)>::value << '\n'; std::cout << std::is_same<decltype(func), void(*)(int)>::value << '\n'; std::cout << std::is_same<decltype(func), void(&)(int)>::value << '\n'; } live demo
the above code homecoming true first row.
no, function lvalue can implicitly converted function pointer per:
§4.3/1 function-to-pointer conversion [conv.func]
an lvalue of function type t can converted prvalue of type “pointer t.” result pointer function.
the relationship between function type a(args...) , reference (namely a(&)(args...)) same relationship between type t , reference (namely t&).
it's used template parameter.
for illustration std::function takes function type stored within std::function object , can declare such object with:
std::function<void(int)> fn; c++ c++11 using
Comments
Post a Comment