R
ralpe
Hi,
I have a template class foo which stores pairs of Keys and Ts in a
vector. Foo has an accessor function at(size_t) that returns a
reference to the pair at the specified index.
I do not want the users of foo to change the first value of a pair,
so I want to return a reference to a pair<const Key, T>, just like
std::map does. I cannot store pairs with a const first value in the
vector because such pairs would not be assignable.
My only solution so far is to use reinterpret_cast. Is this a
portable solution that is guaranteed to work on all platforms?
template<typename Key, typename T>
class foo
{
public:
std:air<const Key, T>& at(std::size_t index)
{
return reinterpret_cast<std:air<const Key, T>&>(pairs_.at(index));
}
void push_back(const std:air<const Key, T>& pair)
{
pairs_.push_back(pair);
}
private:
std::vector<std:air<Key, T> > pairs_;
};
Thanks in advance,
Ralpe
I have a template class foo which stores pairs of Keys and Ts in a
vector. Foo has an accessor function at(size_t) that returns a
reference to the pair at the specified index.
I do not want the users of foo to change the first value of a pair,
so I want to return a reference to a pair<const Key, T>, just like
std::map does. I cannot store pairs with a const first value in the
vector because such pairs would not be assignable.
My only solution so far is to use reinterpret_cast. Is this a
portable solution that is guaranteed to work on all platforms?
template<typename Key, typename T>
class foo
{
public:
std:air<const Key, T>& at(std::size_t index)
{
return reinterpret_cast<std:air<const Key, T>&>(pairs_.at(index));
}
void push_back(const std:air<const Key, T>& pair)
{
pairs_.push_back(pair);
}
private:
std::vector<std:air<Key, T> > pairs_;
};
Thanks in advance,
Ralpe