일반적인 코드를 사용하여 hash_map
/unordered_map
및 list
/vector
컬렉션을 반복하는 우아한 솔루션이 있습니까?다른 유형의 컬렉션을 반복하는 공통 코드
예 : 대신 ch->send(head, cnt)
의 ch.second->send(head, cnt)
:
template<typename collection>
class multicast
{
public:
typedef collection collection_type;
private:
collection_type& m_channels;
public:
multicast(collection_type& channels) : m_channels(channels) { }
void operator()(const buffer::ptr& head, const buffer::ptr& cnt)
{
for each(collection_type::value_type& ch in m_channels)
ch->send(head, cnt); /* this is where the magic should happen? */
}
}
이 코드는 분명히 collection_type
이 collection_type::value_type
이후 unordered_map
때 컴파일에 실패는 실제 값이 달라야에 액세스하십시오 pair
그래서 코드입니다. 그렇다면 중요하지 않은 부분을 제거하는 가장 우아한 방법은 무엇일까요?
템플릿을 부분적으로 특수화 했습니까? – LaC