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 * ...
随机推荐
- 【MSSQL教程】#001 整体思维导图
整个MSSQL体系的一个思维导图,方便理解整个MSSQL需要学习那些方面的知识.
- Linux新建用户没有设置密码
只要你能登陆root账户就行 登陆root账户 输入 echo "密码“ | passwd --stdin 用户名
- mysql中FIND_IN_SET函数的使用
有种需求,A和B是父子关系,B和C是父子关系,C与D亦是父子关系,以此类推,无限级 现在需要查询到某一级(包括本级)下面所有的,就需要用到FIND_IN_SET函数 select * from tab ...
- Hive学习之路 (十三)Hive分析窗口函数(一) SUM,AVG,MIN,MAX
数据准备 数据格式 cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, 创建数据库及表 create datab ...
- MySQL 更改数据库数据存储目录
MySQL数据库默认的数据库文件位于 /var/lib/mysql 下,有时候由于存储规划等原因,需要更改 MySQL 数据库的数据存储目录. 下文总结整理了实践过程的操作步骤. 1 确认MySQ ...
- mysql 5.5.42 更改数据目录 centos 6.5环境
1.新建新数据目录,检查目录属主机权限,一般情况下属于mysql组,myql用户,因为我们安装mysql的时候会新建该账户和组. 2.目录权限检查完毕,停止数据库服务. 3.移动数据目录 ,我用的是m ...
- 初识Qt文件下载
1.新建一个Qt Gui应用,项目名称为http,基类选择为QMainWindow,类名设置为MainWindow. 2.在http.pro文件中的QT += core gui后添加\ networ ...
- C++:引用的简单理解
前言:引用是C++一个很重要的特性,最近看了很多有关引用的资料和博客,故在此对引用的相关知识进行总结 一.什么是引用 引用,顾名思义是某一个变量或对象的别名,对引用的操作与对其所绑定的变量或对象的操作 ...
- ICC Scenario Definition
现代先进工艺下的后端设计都是在 MCMM 情况下设计的,所谓 MCMM 就是 muti-corner muti-mode,用于芯片的不同工作模式和工作条件. 后端设计过程中,需要保证芯片在所有工作模 ...
- highcharts柱状图实现legend和数据列一一对应效果
var cate = ['Green', 'Pink']; Highcharts.chart('container', { chart: { type: 'column' }, legend: { e ...