/// 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;
}

最新文章

  1. 调出CorelDRAW新工具栏的方法
  2. GitHub上新建或删除仓库Repository
  3. Exif的Orientation信息说明
  4. iOS音频篇:AVPlayer的缓存实现
  5. AE 线编辑
  6. C#中的 IList, ICollection ,IEnumerable 和 IEnumerator
  7. java学习——函数
  8. php 针对特殊字符进行转义
  9. hdu 1535 Invitation Cards(SPFA)
  10. accp8.0转换教材第8章JavaScript对象及初识面向对象理解与练习
  11. C++对C的实用性增强
  12. Redmine发布新闻,自动发送邮件功能失效恢复
  13. 00009 - cat、tail、head、tee、wc、sort文件操作和过滤
  14. redis哨兵集群环境搭建
  15. foreach循环赋值问题
  16. jenkins权限设置
  17. 20162325 金立清 S2 W3 C13
  18. Mysql 授权远程访问
  19. phpMyAdmin 应用程序“DEFAULT WEB SITE”中的服务器错误
  20. code3728 联合权值

热门文章

  1. JavaScript动画
  2. MySQL知识总结(三)存储过程
  3. rz -be 上传文件解压失败
  4. Oracle中修改sysman和dbsnmp密码正确流程
  5. objc_msgSend函数的实现
  6. POJ 3294 Life Forms [最长公共子串加强版 后缀数组 &amp;&amp; 二分]
  7. virtualbox+vagrant学习-2(command cli)-19-vagrant box命令
  8. CUDA和OpenGL互操作经典博文赏析和学习
  9. java xml文件
  10. 关于NRF52832能否被替代的详解