Optional Template parameter

后端 未结 2 894
夕颜
夕颜 2020-12-15 04:24

Is it possible to have optional template parameter in C++ , for example

template < class T, class U, class V>
class Test {
};

Here I

2条回答
  •  不思量自难忘°
    2020-12-15 04:59

    Sure, you can have default template parameters:

    template 
    
    template  >
    

    The standard library does this all the time -- most containers take two to five parameters! For example, unordered_map is actually:

    template<
        class Key,                        // needed, key type
        class T,                          // needed, mapped type
        class Hash = std::hash,      // hash functor, defaults to std::hash
        class KeyEqual = std::equal_to, // comparator, defaults to Key::operator==()
        class Allocator = std::allocator> // allocator, defaults to std::allocator
    > class unordered_map;
    

    Typicall you just use it as std::unordered_map without giving it any further thought.

提交回复
热议问题