Trying to define a homogeneous value list with a variadic template

Hi,

I came across the following code which I do not understand:

1
2
template< auto v1, decltype(v1)...VS>
struct HomogeneousValueList  {};


Please explain...

This should not compile but it does in VStudio 2019

 
HomogeneousValueList<11, 22, true, 'a' > hl2;


Regards,
Juan
Well, they're all integers, right? So they're all implicitly convertible to an int.
Try:

 
Homo<11, "hello", true, 'a' > hl2;

Yes, but I do not understand the syntax in :


 
template< auto v1, decltype(v1)...VS>


how does this enforce that all parameter arguments have to be of the same type as the first?
It's just hard to understand this...

You are correct (all were convertible to int in my example), but my question has to do with the above code.


Regards,
Juan
The auto v1 will assign the first type to v1, which in your example is int.
The decltype(v1) is therefore int. So the template becomes:

 
template<int v1, int...VS>

int... VS is a parameter pack: https://en.cppreference.com/w/cpp/language/parameter_pack

Where did you get this code? Post the link.
Last edited on
It's from a book I recently purchased called "C++ 17 The Complete Guide" by Nicolai Josuttis.

Very good book.

I looked at your link for parameter pack but there is nothing with a type before the ...
all the examples use class or typename but none contain a given explicit type like int in our example.

Does int...VS mean int,int,int, ?

template<class ... Types> struct Tuple {};

Would really appreciate a bit more explanation.

Regards,
Juan

I looked at your link for parameter pack but there is nothing with a type before the ...

You didn't look very well, did you. The very first one:

 
type ... Args(optional)

It will take you quite a while to digest that page.
You didn't even try.
I apologize I was in a hurry. You are correct.

thanks and sorry again

Juan
dutch I was trying to speed up my understanding; the link is large with many scenarios. I wanted a straight answer to a very specific question:

Does int...VS expands to a comma separated list of int?

That 's all i wanted to know - I was going to study your link when I have more time.


Registered users can post here. Sign in or register to post.