Back to questions
JuniorTheoryOccasional
What does the program print?
#include <iostream>
template <typename... Ts>
auto count_args(Ts...) {
return sizeof...(Ts);
}
int main() {
std::cout << count_args(1, 2.5, 'x') << ' ' << count_args();
}3 0
- Expecting a size in bytes:
sizeof...(Ts)counts the elements of the pack, three here. It does not add up their sizes assizeofwould. - Expecting
count_args()not to compile: a parameter pack may be empty, and thensizeof...(Ts)is0.
- How would you return the sum of all the arguments with a fold expression?
- How does
sizeof...(Ts)differ fromsizeof(T)for a single typeT?