3 回答
TA貢獻1780條經驗 獲得超4個贊
template <template<class> class H, class S>void f(const H<S> &value) {}HH.
注
std::vector 不typedef value_type.
template <template<class, class> class V, class T, class A>void f(V<T, A> &v) {
// This can be "typename V<T, A>::value_type",
// but we are pretending we don't have it
T temp = v.back();
v.pop_back();
// Do some work on temp
std::cout << temp << std::endl;}注std::vector
f<std::vector, int>(v); // v is of type std::vector<int> using any allocator
f(v); // everything is deduced, f can deal with a vector of any type!
更新auto
template <class Cont>void f(Cont &v) {
auto temp = v.back();
v.pop_back();
// Do some work on temp
std::cout << temp << std::endl;}TA貢獻1982條經驗 獲得超2個贊
template<typename T>static inline std::ostream& operator<<(std::ostream& out, std::list<T> const& v){
out << '[';
if (!v.empty()) {
for (typename std::list<T>::const_iterator i = v.begin(); ;) {
out << *i;
if (++i == v.end())
break;
out << ", ";
}
}
out << ']';
return out;}template<template <typename, typename> class Container, class V, class A>std::ostream& operator<<(std::ostream& out, Container<V, A> const& v)...
#include <iostream>#include <vector>#include <deque>#include <list>template<typename T, template<class,class...> class C, class... Args>std::ostream& operator <<(std::ostream& os, const C<T,Args...>& objs){
os << __PRETTY_FUNCTION__ << '\n';
for (auto const& obj : objs)
os << obj << ' ';
return os;}int main(){
std::vector<float> vf { 1.1, 2.2, 3.3, 4.4 };
std::cout << vf << '\n';
std::list<char> lc { 'a', 'b', 'c', 'd' };
std::cout << lc << '\n';
std::deque<int> di { 1, 2, 3, 4 };
std::cout << di << '\n';
return 0;}輸出量
std::ostream &operator<<(std::ostream &, const C<T, Args...> &) [T = float, C = vector, Args = <std::__1::allocator<float>>]1.1 2.2 3.3 4.4 std::ostream &operator<<(std::ostream &, const C<T, Args...> &) [T = char, C = list, Args = <std::__1::allocator<char>>]a b c d std::ostream &operator<<(std::ostream &, const C<T, Args...> &) [T = int, C = deque, Args = <std::__1::allocator<int>>]1 2 3 4
TA貢獻1831條經驗 獲得超9個贊
// Library codetemplate <template <class> class CreationPolicy>class WidgetManager : public CreationPolicy<Widget>{
...};typedef WidgetManager<MyCreationPolicy> MyWidgetMgr;
typedef WidgetManager< MyCreationPolicy<Widget> > MyWidgetMgr;
- 3 回答
- 0 關注
- 736 瀏覽
添加回答
舉報
