variadic templates & pass by const reference & member operator [] in const map & gcc sucks
/// 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的更多相关文章
- C++11 : variadic templates(可变参数模板)
Introduction: Before the possibilities of the new C++ language standard, C++11, the use of templat ...
- 【C/C++】C++11 Variadic Templates
Variadic Templates 1.function template:利用“参数个数逐一递减”的特性,实现递归函数调用 template <typename T, typename... ...
- C++11_ Variadic Templates
版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的又一个新特性 Variadic Templates (可变模板参数) 它的实现类似于initializer_list< ...
- When do we pass arguments by reference or pointer?
在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...
- 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);* ...
- C++2.0新特性(一)——<特性认知、__cplusplus宏开启、Variadic Templates 、左右值区分>
一.新特性介绍 2.0新特性包含了C++11和C++14的部分 1.2 启用测试c++11功能 C++ 标准特定版本的支持,/Zc:__cplusplus 编译器选项启用 __cplusplus 预处 ...
- C++11新特性之operator "" xxx(const char *, size_t n)
从C++11开始,我们可以使用以下形式通过常量字符串构造自定义类型, 比如: class Person { public: Person(const std::string& name): _ ...
- could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...
- const int * p 和 int const * p 和 int * const p 的区别
首先注意,const int * p 和int const *p 是一样的,并且不管是不是*p,即使const int i和int const i也是一样的,所以我们接下来只讨论int const * ...
随机推荐
- PHP设计模式系列 - 中介者模式
中介者模式 中介者模式用于开发一个对象,这个对象能够在类似对象相互之间不直接相互的情况下传送或者调解对这些对象的集合的修改.一般处理具有类似属性,需要保持同步的非耦合对象时,最佳的做法就是中介者模式. ...
- Weblogic 设置优先引用web项目的jar包
在WEB-INF/weblogic.xml中进行如下配置: <container-descriptor> <prefer-web-inf-classes>true</pr ...
- 浅析JS中的堆内存与栈内存
最近跟着组里的大佬面试碰到这么一个问题, Q:说说var.let.const的区别 A:balabalabalabla... Q:const定义的值能改么? A:你逗我?不能吧 不知道各位看官怎么想? ...
- 1084. [SCOI2005]最大子矩阵【网格DP】
Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵 不能相互重叠. Input 第一行为n,m,k(1≤n≤100,1≤m≤ ...
- 文件上传之MultipartFile使用
转载 文件断点上传,html5实现前端,java实现服务器 一.单/多文件上传使用例子: 工程路径如下 -src |--main.java --controller --service ...
- Mysql安装(win10 64位)
公司的测试数据库只有读的权限,而且还不能用IP和端口去访问,所有很多时候不方便(尤其是想练手的时候).闲着也是闲着,自己搭建一个Mysql数据库出来.以下操作,全部基于win10专业版 64位,仅供参 ...
- ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)
B - Benny's Compiler Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- unlink与close关系
close和unlink.以前时候总是不太理解两者的区别,最近看到一篇博客比较详细地描述了二者的本质区别,这里我引用了它的原文. “每一个文件,都可以通过一个struct stat的结 ...
- swoft orm中的坑(针对实体类的属性名称和数据库字段不相等)
最近在用swoft的orm,发现了一些问题: 首先看下实体类的定义 它的属性名称和所映射的数据库字段名不一致,这个就会导致蛋疼的问题,首先,在我们使用orm的时候,应该使用哪个字段? 我直接说结论,在 ...
- Hive--可执行SQL的Hadoop数据仓库管理工具
Hive是一个基于HDFS的数据仓库软件,可理解为数据库管理工具:Hive的功能主要有: 1. 支持使用SQL对分布式存储的大型数据集进行读.写.管理,将SQL转化成MapReduce任务执行: 2. ...