可以将非const值类型的映射强制转换为const值类型的映射吗?
Can I cast a map with a non-const value type to one with a const value type?
我有如下内容:
map<int, StructType> map;
const map<int, StructType>& GetMap() { return map; }
我想这样做:
const map<int, const StructType>& GetConstMap() { return map; }
我是否可以将这个const
-ness添加到映射的值类型中?
std::map
的接口设计使const map<K,T>
有效地具有const值类型,通过从不暴露对其元素的非const访问。
因此不能通过const map
引用添加、删除或修改元素。
:
struct X
{
map<int, StructType> m;
const map<int, StructType>& GetConstMap() const { return m; }
}
就是你想要的
相关文章:
- 将类型映射到整数值后,如何在给定整数值的情况下恢复类型?
- C++17:在编译时将类型映射到整数值
- 为C++重载函数创建SWIG类型映射
- 返回各种类型的函数的SWIG类型映射
- 类型映射资源并将列表转换为矢量(并返回)
- 有没有办法在 C++17 中创建编译时类型映射以进行类型检查?
- 如何在OpenCV中使用双类型映射进行重新映射
- SWIG 多参数类型映射适用于函数,但如果有多个构造函数,则不适用于构造函数
- Swig:如何类型映射c ++字符串常量和python字符串?
- SWIG 类型映射的 python 列表到双倍 *
- SWIG -- 在扩展中使用类型映射
- 嵌入的多个变体类型映射
- SWIG类型映射中的内存释放
- 您将如何为 std::list<std::string> 创建一个类型映射,以在<String> List in C++ 中为 Java 在 SWIG 中创建?
- SWIG-为具有四个参数的函数创建一个类型映射,该参数传递一个字节数组
- 在C++中将类类型映射到其他类类型
- C func 到C++具有非双射参数类型映射的实例成员蹦床
- Swig 界面中的类型映射错误
- 如何在 swig 中引用类型映射类
- 将实例变量类型映射到模板成员函数