実装の一部としてキューを使用する関数を作成することに興味がありますが、テンプレート化されたキューのタイプに基づいて異なる機能を持つように、キューのタイプでテンプレート化したいと考えています。
基本的な例を次に示します。
template <typename Queue>
void example()
{
Queue a;
a.push(3);
a.push(1);
a.push(2);
while (!a.empty()) {
cout << a.top() << ' ';
a.pop();
}
cout << flush;
}
私が欲しいのは、example<stack<int>>()
印刷すること2 1 3
、example<priority_queue<int>>()
印刷すること3 2 1
、example<queue<int>>()
印刷すること3 1 2
です。これはstack
andに対して機能しますpriority_queue
が、残念ながらqueue
提供されずtop
、代わりに提供されますfront
。代わりにtop
toqueue
を呼び出すときにコンパイラに通知する簡単な方法はありますか?front
これを回避する唯一の方法は、この質問How to implement generic method for STL containers that have`t common interface needed for that method using template template parametertop
に従い、データ型ごとに独自のローカルを実装することです。代わりにそれを呼び出します。ただし、このソリューションは非常に洗練されていないように思えます。可能であれば、別の方法を見つけたいと思います。
編集:正確にはC++ 11、gcc 4.7.0をサポートするコンパイラを使用しています。