Search code examples
c++templatestypename

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?


Solution

  • 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