rencedm112
rencedm112

Reputation: 587

What does "typename..." mean in this context?

I found some code in cppreference.com that I don't understand. Here is the link: Type Alias. It's talking about dependent template-id which I don't understand. Here's the code:

//When the result of specializing an alias template is a dependent template-id, 
//subsequent substitutions apply to that template-id:

template<typename...>
using void_t = void;
template<typename T>
void_t<typename T::foo> f();
f<int>();     // error, int does not have a nested type foo

When I hover over it on VS 2019 it says void_t<<unnamed>...>

Can anyone explain to me how is this unnamed typename useful?

Upvotes: 1

Views: 182

Answers (3)

TruthSeeker
TruthSeeker

Reputation: 1579

Let me try to explain, Expression template<typename...> using void_t = void; with type deduced to void irrespective of passed template parameter. example,

template<typename...>
   using void_t = void;

template <typename T>
void_t<T> fun1() {
    std::cout << "fun1 called" << std::endl;
}

int main() {
    fun1<int>();   //fun1 called
    fun1<float>(); //fun1 called
}

To extend the same, template<typename T>void_t<typename T::foo> f(); only accepts typename T which has nested type T::foo. For example,

template<typename...> 
   using void_t = void;

template<typename T>
        void_t<typename T::foo> f() {}

struct bar
{
    typedef int foo;
};
int main() {
    f<bar>(); //Valid expression as bar::foo is nested type of bar
    f<int>(); //Error because `int::foo`is not a nested type of `int`.
}

For more information refer Substitution failure is not an error

Upvotes: 1

Andreas DM
Andreas DM

Reputation: 11018

template<typename...>
using void_t = void;

It allows you to pass template arguments to it and it will just eat up all the arguments, resulting in the same type. It is useful for SFINAE

Upvotes: 1

Thomas Sablik
Thomas Sablik

Reputation: 16448

It's a template pack without name because it's not used. You can pass any types and the result will be the same.

Upvotes: 4

Related Questions