j__
j__

Reputation: 740

Non-type template arguments of Function by reference or default?

Should I take nttp functions by reference or by "value" (implicit pointer)?

template< auto fn >
struct c
{
    static constexpr auto functor = fn;
};

vs

template< auto & fn >
struct c
{
    static constexpr auto functor = fn;
    static constexpr auto && functor = fn; // maybe?
};

Or does it not matter? If it is compiler dependent, let the question be about MSVC

Usage:

c< GetLastError >::functor( );

Upvotes: 0

Views: 91

Answers (1)

AndyG
AndyG

Reputation: 41092

You are passing a function to a template as a non type template argument. It will always refer to the same function, because it's actually the function's address. In this case, auto is fine.

Usually people use auto for their needs, but you may use a pointer or a reference to refer to global data (e.g., variables with file level scoping).

Upvotes: 1

Related Questions