vendredi 23 juin 2017

How is a template function evaluated by a compiler?

Consider the following variadic template function:

template<typename T, typename... Args>
auto foo(Args... args) -> T[sizeof...(args)]
{
   T t[sizeof...(args)];
   // Maybe do some pack expansion/assignment
   return t;
}

with the instantiation:

// total should be of type int[5];
auto total = foo(1,2,3,4,5);

I understand that this will not compile, due to the return type not being deducible, but I do not understand why it is not deducible.

Is there something about this function that the compiler does/can not know at compile time, or the order of parts of the function being evaluated, which prevents type deduction?

I suspect it is due to the calling of the function sizeof..., which I think evaluates at run-time. If so, is there a compile-time equivalent?

Aucun commentaire:

Enregistrer un commentaire