variadic templates & pass by const reference & member operator [] in const map & gcc sucks
2024-10-19 03:33:15
/// bugs code with comments
#include <iostream>
#include <memory>
#include <unordered_map>
using namespace std;
class A
{
public:
A(const std::unordered_map<int, int > &ref) : ref_m(ref) {}
void test1(int index) {
// std::cout << ref_m[index] << std::endl; // bugs version in c++ specification, mainly sucks in gcc compile error prompts.
std::cout << ref_m.at(index) << std::endl; // or ref_m.find() etc.
}
// this function will crash, as a result of ref_m get a wrong memory address by value passing instead of reference passing in gcc
void test2() {
for(auto& e: ref_m)
std::cout << e.first << ' '<< e.second << std::endl;
}
void print_ref_size() {
std::cout << "ref_m size: " << ref_m.size() << std::endl;
}
private:
const std::unordered_map<int, int >& ref_m;
};
template <typename ManipulatorType, typename ... Args>
std::shared_ptr<ManipulatorType> CreateTester(std::string name, Args ... args)
{
return std::make_shared<ManipulatorType>(args ...);
}
class B{
public:
B(){}
std::shared_ptr<A> CreateATesterWrapper() {
// return CreateTester<A>("uselessparams", ref); // bugs version in gcc
return CreateTester<A>("uselessparams", std::ref(ref)); // or replace "Args ... args" with "Args& ... args" in gcc
}
void print_ref_size() {
std::cout << "ref size: " << ref.size() << std::endl;
}
std::unordered_map<int, int > ref = {{1,2}};
};
int main(int argc, char* argv[])
{
B b;
auto a = b.CreateATesterWrapper();
b.print_ref_size();
a->print_ref_size();
a->test1(1);
a->test2();
return 0;
}
最新文章
- 调出CorelDRAW新工具栏的方法
- GitHub上新建或删除仓库Repository
- Exif的Orientation信息说明
- iOS音频篇:AVPlayer的缓存实现
- AE 线编辑
- C#中的 IList, ICollection ,IEnumerable 和 IEnumerator
- java学习——函数
- php 针对特殊字符进行转义
- hdu 1535 Invitation Cards(SPFA)
- accp8.0转换教材第8章JavaScript对象及初识面向对象理解与练习
- C++对C的实用性增强
- Redmine发布新闻,自动发送邮件功能失效恢复
- 00009 - cat、tail、head、tee、wc、sort文件操作和过滤
- redis哨兵集群环境搭建
- foreach循环赋值问题
- jenkins权限设置
- 20162325 金立清 S2 W3 C13
- Mysql 授权远程访问
- phpMyAdmin 应用程序“DEFAULT WEB SITE”中的服务器错误
- code3728 联合权值