The difference between the typename keyword and the class keyword in the template

Asked 2 years ago, Updated 2 years ago, 74 views

I've seen both of them a lot when defining templates, what's the difference between these two?

And the other question is, I saw a template like this on Wikipedia What does each keyword mean here?

template < template < typename, typename > class Container, typename Type >
class Example
{
     Container< Type, std::allocator < Type > > baz;
};

c++ template keywords

2022-09-22 22:25

1 Answers

Usually, you can use either typename or class.

template<class T>
class Foo{};

///Two are the same

template<typename T>
class Foo{};

Special cases where the two cannot be mixed

If you are writing types related to other template parameters, you can only write the typename keyword.

template<typename param_t>
class Foo
{
    typedef typename param_t::baz sub_t;
};

The second question is here.

template template must use the class keyword as follows, and cannot be mixed with typename at this time template < template < typename, typename > class Container, typename Type >

In addition, when instantiating a template, you can only write class as follows: template class Foo<int>;


2022-09-22 22:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.