cppannotations/annotations/yo/functiontemplates/local.yo

53 lines
1.2 KiB
Text

Usually, types have names. But an emi(anonymous type) may also be defined:
verb(
enum
{
V1,
V2,
V3
};
)
Here, the tt(enum) defines an hi(unnamed type) em(unnamed) or
hi(type: anonymous, local)
emi(anonymous type).
When defining a function template, the compiler normally
deducts the types of its template type parameters from its arguments:
verb(
template <typename T>
void fun(T &&t);
fun(3); // T is int
fun('c'); // T is char
)
The following, however, can also be used:
verb(
fun(V1); // T is a value of the above enum type
)
Within tt(fun) a tt(T) variable may be defined, even if it's an anonymous
type:
verb(
template <typename T>
void fun(T &&t)
{
T var(t);
}
)
Values or objects of
hi(local type)
locally defined types may also be passed as arguments to function
templates. E.g.,
verb(
void definer()
{
struct Local
{
double dVar;
int iVar;
};
Local local; // using a local type
fun(local); // OK: T is 'Local'
}
)