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

variadic templates & pass by const reference & member operator [] in const map & gcc sucks的更多相关文章

  1. C++11 : variadic templates(可变参数模板)

      Introduction: Before the possibilities of the new C++ language standard, C++11, the use of templat ...

  2. 【C/C++】C++11 Variadic Templates

    Variadic Templates 1.function template:利用“参数个数逐一递减”的特性,实现递归函数调用 template <typename T, typename... ...

  3. C++11_ Variadic Templates

    版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的又一个新特性 Variadic Templates (可变模板参数) 它的实现类似于initializer_list< ...

  4. When do we pass arguments by reference or pointer?

    在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...

  5. error C2556: 'const char &MyString::operator [](int)' : overloaded function differs only by return type from 'char &MyString::operator [](int)'

    char & operator[](int i);const char & operator[](int i);/*const char & operator(int i);* ...

  6. C++2.0新特性(一)——<特性认知、__cplusplus宏开启、Variadic Templates 、左右值区分>

    一.新特性介绍 2.0新特性包含了C++11和C++14的部分 1.2 启用测试c++11功能 C++ 标准特定版本的支持,/Zc:__cplusplus 编译器选项启用 __cplusplus 预处 ...

  7. C++11新特性之operator "" xxx(const char *, size_t n)

    从C++11开始,我们可以使用以下形式通过常量字符串构造自定义类型, 比如: class Person { public: Person(const std::string& name): _ ...

  8. could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

    VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...

  9. const int * p 和 int const * p 和 int * const p 的区别

    首先注意,const int * p 和int const *p 是一样的,并且不管是不是*p,即使const int i和int const i也是一样的,所以我们接下来只讨论int const * ...

随机推荐

  1. System.Data.DbType与各类数据库的数据类型的映射关系

    有如下类型的映射对照: System.Data.SqlClient.SqlDbType System.Data.OleDb.OleDbType System.Data.Odbc.OdbcType Sy ...

  2. 《面向对象程序设计》 三 Calculator 计算器初步

    Git传送门 纸上得来终觉浅,绝知此事要躬行. 学习了一些c++的相应知识后,虽然了解写法,真正操作时还是出现小错误.要多操作,记住关键点,避免不该有的小错误. 写分文件类外定义时出现了问题.在Sca ...

  3. TeamViewer app案例分析

    产品 产品名 TeamViewer远程app 选择原因 远程连接软件是不时之需,当有时私人电脑没有在身边而又需要操作电脑时,远程控制TeamViewer这个软件能帮我们大忙. 调研与评测 1.第一次上 ...

  4. 学习日期时间控件 daterangepicker

    aterangepicker 是一款日期时间控件,可选择“年,月,日,时,分,秒”,可选择单面板,也可选择双面板(起止时间). 单面板示例:daterangepicker 单面板 codepen 在线 ...

  5. 给 Virtualbox 中 Ubuntu 系统设置静态 IP

    虚拟机网络选择 桥接网卡 模式. 主要涉及两个步骤: 1. 修改 /etc/network/interfaces 文件: 2. 修改 dns : 第一步,修改 interfaces 文件: sudo ...

  6. linux下添加用户到sudo组 并禁止sudo用户修改密码

    linux下添加用户到sudo组 创建用户  useradd hanli 为新用户设置密码  passwd hanli 创建用户组  groupadd  op 将用户添加到用户组  usermod - ...

  7. MySQL - FEDERATED引擎实现跨服务器查询

    1. MySQL插件的安装与卸载 # 查看插件信息 mysql> show plugins; mysql> select plugin_name,plugin_status,plugin_ ...

  8. Hive学习之路 (十二)Hive SQL练习之影评案例

    案例说明 现有如此三份数据:1.users.dat 数据格式为: 2::M::56::16::70072, 共有6040条数据对应字段为:UserID BigInt, Gender String, A ...

  9. Spring整合MyBatis(四)MapperFactoryBean 的创建

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.MapperFactoryBean的初始化 二.获取 Map ...

  10. 传入一个integer数组,取出最大、最小值

    /** * <p> * 传入一个integer数组,取出最大值 * </p> * @author yunns 2015年11月19日 * @param array * @ret ...