Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

c++11 - How can I write a template function that takes a generic C++ standard library container

I have a function that needs to be able to work for different container types, for example

void foo(const std::vector<bar>& param1, const std::vector<double>& params2)

and

void foo(const std::list<bar>& param1, const std::list<double>& params2)

where bar is a class that I've written. The function body itself uses generic C++ standard library functions.

Is there a way I can templatise this? I have tried

template<typename T> void foo(const T<bar>&, const T<double>&)

But this gives the compiler error

error C2988: unrecognizable template declaration/definition

I'm using MSVC2015.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should declare T as template template parameter to indicate that it's a template-name (and needs arguments to be instantiated), e.g.

template<template <typename...> class T> 
void foo(const T<bar>&, const T<double>&);

LIVE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...