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 * ...
随机推荐
- session过期,拦截ajax请求并跳转登录页面
1.方法一 :1.1使用filter 和ajaxsetup 对ajax进行拦截并跳转登录页面 public void doFilter(ServletRequest request, ServletR ...
- 【教程】【FLEX】#005 拖动
在Flex中,组件的拖动分为: 1. 加强型(即本身就可以拖动设置是否可以拖动的属 [dragEnabled ,dropEnabled ] 即可) 2. 非加强型(可以通过DragManager,Dr ...
- 到底该用img还是background-image?
在前端页面的实现过程中,我们经常会遇到这个情况:有一个盒子,盒子里面需要放一张图片.这个时候,我们既可以通过添加image标签来实现,也可以通过设置背景图的形式实现,哪种更好呢? 一般情况下,可能就是 ...
- 什么是HOOK(钩子):消息拦截与处理
对于Windows系统,它是建立在事件驱动机制上的,说白了就是整个系统都是通过消息传递实现的.hook(钩子)是一种特殊的消息处理机制,它可以监视系统或者进程中的各种事件消息,截获发往目标窗口的消息并 ...
- Laravel中如何将单个routes.php分割成多个子文件
随着业务逻辑越来越复杂,routes.php文件也会变得越来越庞大,为了便于管理,我们可以像管理配置文件那样将其分割成多个子文件,这实现起来很简单: // app/routes.php ... // ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- Day2 Mybatis初识(二)
mapper接口开发 传统dao的开发问题(ibatis) 方法调用:字符串易错,硬编码 mapper代理开发 a) 编写全局配置 b) 编写接口(自动根据接口和映射文件创建实现类) c) 编写映射文 ...
- SSM框架之整合EhCache
本文主要讲ssm框架整合EhCache.或许有人会问什么是ehcache. 关于EhCache的介绍和优劣,我想下面的解答足以让读者明白: EhCache 是一个纯Java的进程内缓存框架,具有快速. ...
- Python自动化之高级语法单例模式
方法1 共享属性;所谓单例就是所有引用(实例.对象)拥有相同的状态(属性)和行为(方法) 同一个类的所有实例天然拥有相同的行为(方法), 只需要保证同一个类的所有实例具有相同的状态(属性)即可 所有实 ...
- Kafka设计解析(十七)Kafka 0.11客户端集群管理工具AdminClient
转载自 huxihx,原文链接 Kafka 0.11客户端集群管理工具AdminClient 很多用户都有直接使用程序API操作Kafka集群的需求.在0.11版本之前,kafka的服务器端代码(即添 ...