C ++ function template compiles error "containerType is not a template"

I am trying to write a function to “pull” parameters for logging purposes. For example, I would like to write something like this:

vector<string> queries; 
set<uint_8> filters;
LOG(INFO) << stringify<vector, string>(queries);
LOG(INFO) << stringify<set, uint_8>(filters);

Here is the function template that I wrote:

template <typename containerType, typename elemType>
string _stringify(const string name, const containerType<elemType> &elems) {
    ostringstream os;
    os << name << ": [";
    BOOST_FOREACH(elemType elem, elems) {
        os << elem << ",";    
    }
    os << "]";
    return os.str();
} 

Here is the error message I received: error: ‘containerType’ is not a template

Thanks Alex

+3
source share
1 answer

You need to use the template template parameter, for example,

template <template <typename> class containerType, typename elemType>
string _stringify(const string name, const containerType<elemType>& elems)

Please note: if you plan to use this with standard library containers, most of them have several template parameters (for example, sequence containers have two: one for the value type and one for the dispenser type).

, ( ) value_type typedef, . ,

template <typename ContainerT> 
void f(const ContainerT& c)
{
    typedef typename ContainerT::value_type ElementT;  
    // Use ContainerT and ElementT
}
+9

Source: https://habr.com/ru/post/1758651/


All Articles