There is a structure TA
template <typename T>
struct TA
{
typedef std::vector <T> Type;
};
and test() function having a default parameter of the type TA.
template <typename T>
void test ( typename TA<T>::Type a1,
typename TA<T>::Type a2 = typename TA<T>::Type(a1.size()) )
{}
Is it posssible to use a1.size() in default parameter a2 definition?
int main()
{
TA <double> ::Type a1;
test<double>(a1);
}
Is it posssible to use a1.size() in default parameter a2 definition?
No. It is forbidden by the Standard. You cannot use function parameter to set the default value of other parameter.
§8.3.6/9 (C++03) explicitly says,
Default arguments are evaluated each time the function is called. The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in default argument expressions, even if they are not evaluated.
So the solution is: use overload:
template <typename T>
void test(typename TAs<T>::Type a)
{
test(a, typename TA<T>::Type(a.size()));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With