便携式并保存reinterpret_cast? [英] portable and save reinterpret_cast?
问题描述
我有一个模板类foo,它在
向量中存储Keys和Ts对。 Foo在(size_t)有一个访问函数,它返回一个
对指定索引处对的引用。
我不希望foo的用户更改一对的第一个值,
所以我想返回对一对< const Key,T>的引用,就像
std :: map一样。我无法在
向量中存储具有const第一个值的对,因为这样的对不可分配。
到目前为止我唯一的解决方案是使用reinterpret_cast。这是一个
便携式解决方案,保证可以在所有平台上运行吗?
模板< typename键,typename T>
class foo
{
public:
std :: pair< const Key,T>& at(std :: size_t index)
{
return reinterpret_cast< std :: pair< const Key,T>&>(pairs_.at(index)) ;
}
void push_back(const std :: pair< const Key,T>& pair)
{
pairs_.push_back(对);
}
私人:
std :: vector< std :: pair< Key,T pairs_;
};
提前致谢,
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::pair<const Key, T>& at(std::size_t index)
{
return reinterpret_cast<std::pair<const Key, T>&>(pairs_.at(index));
}
void push_back(const std::pair<const Key, T>& pair)
{
pairs_.push_back(pair);
}
private:
std::vector<std::pair<Key, T pairs_;
};
Thanks in advance,
Ralpe
推荐答案
ralpe napsal:
ralpe napsal:
我有一个模板类foo,它在
向量中存储Keys和Ts对。 Foo在(size_t)有一个访问函数,它返回一个
对指定索引处对的引用。
我不希望foo的用户更改一对的第一个值,
所以我想返回对一对< const Key,T>的引用,就像
std :: map一样。我无法在
向量中存储具有const第一个值的对,因为这样的对不可分配。
到目前为止我唯一的解决方案是使用reinterpret_cast。这是一个
便携式解决方案,保证可以在所有平台上运行吗?
模板< typename键,typename T>
class foo
{
public:
std :: pair< const Key,T>& at(std :: size_t index)
{
return reinterpret_cast< std :: pair< const Key,T>&>(pairs_.at(index)) ;
}
void push_back(const std :: pair< const Key,T>& pair)
{
pairs_.push_back(对);
}
私人:
std :: vector< std :: pair< Key,T pairs_;
};
提前致谢,
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::pair<const Key, T>& at(std::size_t index)
{
return reinterpret_cast<std::pair<const Key, T>&>(pairs_.at(index));
}
void push_back(const std::pair<const Key, T>& pair)
{
pairs_.push_back(pair);
}
private:
std::vector<std::pair<Key, T pairs_;
};
Thanks in advance,
Ralpe
我认为更简单的解决方案是返回std :: pair< const something,
something_elseinitalized with std :: pair< something,something_else>
值:
std :: pair< const int,intTest()
{
std :: pair< int,intp(1,2); //这只是为了说明
返回p;
}
I think that simpler solution is to return std::pair<const something,
something_elseinitalized with std::pair<something, something_else>
value:
std::pair<const int, intTest()
{
std::pair<int, intp(1, 2); // This is only for illustration
return p;
}
Ondra Holub写道:
Ondra Holub wrote:
我认为更简单的解决方案是返回std :: pair< const something,
something_elseinitalized with std :: pair< something,something_else> ;
值:
std :: pair< const int,intTest()
{
std :: pair< int,intp(1,2); //这只是为了说明
返回p;
}
I think that simpler solution is to return std::pair<const something,
something_elseinitalized with std::pair<something, something_else>
value:
std::pair<const int, intTest()
{
std::pair<int, intp(1, 2); // This is only for illustration
return p;
}
不,这不是解决方案因为我需要返回一个参考,使得
这可能:
myFoo.at(42).second = 4711;
>
ralpe napsal:
ralpe napsal:
Ondra Holub写道:
Ondra Holub wrote:
我认为更简单的解决方案是返回std :: pair< const something,
something_elseinitalized with std :: pair< something,something_else>
值:
std :: pair< const int,intTest()
{
std :: pair< int ,intp(1,2); //这只是为了说明
返回p;
}
I think that simpler solution is to return std::pair<const something,
something_elseinitalized with std::pair<something, something_else>
value:
std::pair<const int, intTest()
{
std::pair<int, intp(1, 2); // This is only for illustration
return p;
}
不,这不是解决方案因为我需要返回一个引用来实现
这可能:
myFoo.at(42).second = 4711;
No, that is no a solution because I need to return a reference to make
this possible:
myFoo.at(42).second = 4711;
你是对的,参考文献有问题。我认为
reinterpret_cast应该适用于这种情况,因为
类型仅在const中有所不同,所以它们应该完全相同
具有相同对齐的位置。我不认为,const可能会导致一些放置或对齐优化,但我不确定。
也许100%安全的解决方案是改变API并且不返回对。
You''re right, with references it is problem. I think that
reinterpret_cast should work everywhere for this case, becasue both
types differ only in const, so they should occupy exactly the same
place with the same alignment. I do not think, that const could lead to
some placement or alignment optimizations, but I am not sure.
Maybe 100% safe solution is to change API and do not return pair.
这篇关于便携式并保存reinterpret_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!