/// 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. 使用 FRP 反向代理实现 Windows 远程连接

    互联网普及率的日渐攀升与 IPv4 资源的持续减少,现在大部分家庭宽带都不会分配公网 IP ,这使一些网络应用的实现多了些困难,像个人的 NAS 和一些智能家居设备.对于分配公网 IP ,各地运营商的 ...

  2. 解析JS运动

    解析JS运动 物体运动原理:通过改变物体的位置,而发生移动变化. 任何运动都是相对的,就像物理中的运动公式:s(要达到的)=s0(当前的样式值)+vt. 方法:      1.运动的物体使用绝对定位 ...

  3. python第十四课--排序及自定义函数之自定义函数(案例五)

    演示函数的定义和使用细节: 默认参数:#在设计自定义函数的时候,就存在一个默认值,就算在调用的时候不显示的传入实参,也不会报错.#会用默认值来代替参与后期的运算 def m1(name='张三',ag ...

  4. 「GXOI / GZOI2019」旅行者

    题目 我还是太傻了 考虑每一条边的贡献,对于一条有向边\((u,v,w)\),我们求出\(k\)个关键点中到\(u\)最近的距离\(dis_1\),以及\(v\)到\(k\)个关键点中最近的距离\(d ...

  5. 隐马尔可夫(HMM)模型

    隐马尔可夫(HMM)模型 隐马尔可夫模型,是一种概率图模型,一种著名的有向图模型,一种判别式模型.主要用于时许数据建模,在语音识别.自然语言处理等领域广泛应用. 概率图模型分为两类,一类:使用有向无环 ...

  6. 20145203盖泽双java实验三 敏捷开发与XP实践

    java实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> ...

  7. SSM框架之批量增加示例(同步请求jsp视图解析)

    准备环境:SSM框架+JDK8/JDK7+MySQL5.7+MAVEN3以上+Tomcat8/7应用服务器 示例说明: 分发给用户优惠券,通过checkbox选中批量分发,对应也就是批量增加. 对于公 ...

  8. CMD centos7 安装 最新版本的docker -- dockerfire 原语 ENTRYPOINT - 导入镜像 tar mariadb Dockerfile 构建镜像

    yum update # vim /etc/yum.repos.d/docker.repo //添加以下内容 [dockerrepo] name=Docker Repository baseurl=h ...

  9. NRF52832 能烧写代码 但是不运行 ,是因为没有烧写协议栈

    仿真进入的状态 , MOVES R0.R0 请烧写  协议栈 ,自行百度!

  10. 你都用python来做什么?

    首页发现话题   提问     你都用 Python 来做什么? 关注问题写回答     编程语言 Python 编程 Python 入门 Python 开发 你都用 Python 来做什么? 发现很 ...