Blog Archive

Sunday, July 28, 2019

How to solve the error with passing const std::unordered_map or passing const std::map

// Example program
#include <iostream> #include <string> #include<unordered_map> using namespace std; void printMap(const unordered_map<int, int> m){ for(auto item:m){ cout<< item.first << " " << item.second<<" "; cout<< item.first << " " << m[item.first]<<endl; //cout<< item.first << " " << m.at(item.first)<<endl; } return; } int main() { unordered_map<int, int> m; m[0]=10; m[1]=20; printMap(m); return 0; }


Question: How to solve the following error:
9:49: error: passing 'const std::unordered_map<int, int>' as 'this' argument of
'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type&
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type&)
[with _Key = int; _Tp = int; _Hash = std::hash<int>; _Pred = std::equal_to<int>;
_Alloc = std::allocator<std::pair<const int, int> >;
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = int;
std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = int]'
discards qualifiers [-fpermissive]


Answer:
using at() to access the element of map or unordered_map
Solution:
cpp.sh/7rmyz


Sunday, July 21, 2019

Constant pointer vs Pointer to constant [C++]



https://stackoverflow.com/questions/21476869/constant-pointer-vs-pointer-to-constant


const int* ptr; 
declares ptr a pointer to const int type. You can modify ptr itself but the object pointed to by ptr shall not be modified.
const int a = 10;
const int* ptr = &a;  
*ptr = 5; // wrong
ptr++;    // right  
While
int * const ptr;  
declares ptr a const pointer to int type. You are not allowed to modify ptr but the object pointed to by ptr.
int a = 10;
int *const ptr = &a;  
*ptr = 5; // right
ptr++;    // wrong
Generally I would prefer the declaration like this which make it easy to read and understand (read from right to left):
int const  *ptr; // ptr is a pointer to constant int 
int *const ptr;  // ptr is a constant pointer to int

Friday, July 12, 2019

2019年你不需要服务器了:Serverless的无剑境界

source: http://www.mitbbs.com/article_t1/Programming/31553555_0_1.html

发信站: BBS 未名空间站 (Fri Jul 12 00:09:01 2019, 美东)

简单易懂教程:https://serverless-stack.com/
教程Demo: https://demo-user-mgmt.serverless-stack.com/

今天参加了一个AWS纽约的展览会,突然发现大公司只用纯Amazon Lambda/DynamoDB/S3
等一堆积木(马鬃各种积木任选)就能搭完整网站了。 连Node/Express都省下了。 跟
着这个tutorial走了一遍感觉非常干净:React前端,马鬃一系列服务后端。

最近十年从:

1)本地服务器 
2)云服务器
3)Docker容器
到最后的
4)Amazon Lambda

真的不得了。  有做过Serverless项目的人说说经验?  成本据说比服务器便宜很多,
Lambda是跑一次收一次费用。Idle time不收钱。

Tuesday, July 9, 2019

Defensive programming

source:
https://akrzemi1.wordpress.com/2015/01/12/defensive-programming/

Have you ever asked yourself, or participated in a discussion on whether “defensive programming”is a good or a bad thing? The issue is controversial, and recently, while watching talk “Defensive Programming Done Right, Part I” by John Lakos, I realized (I think) why. Term “undefined behavior” means different things to different people.
Rather than saying something myself, let me ask you a question. Given the following initial code:
1
2
3
4
5
6
7
bool is_monitored(const Monitor* m, const TaskId* t)
{
  auto it = std::find(m->monitored_tasks().begin(),
                      m->monitored_tasks().end(),
                      *t);
  return it != m->monitored_tasks().end();
}
Assuming that we never expect m or t to be null, which of the following variants would you call a “defensive programming” technique?
Variant A:
1
2
3
4
5
6
7
8
9
10
bool is_monitored(const Monitor* m, const TaskId* t)
{
  if (m == nullptr) return false;
  if (t == nullptr) return false;
 
  auto it = std::find(m->monitored_tasks().begin(),
                      m->monitored_tasks().end(),
                      *t);
  return it != m->monitored_tasks().end();
}
Variant B:
1
2
3
4
5
6
7
8
9
10
bool is_monitored(const Monitor* m, const TaskId* t)
{
  if (m == nullptr) throw std::logic_error("...");
  if (t == nullptr) throw std::logic_error("...");
 
  auto it = std::find(m->monitored_tasks().begin(),
                      m->monitored_tasks().end(),
                      *t);
  return it != m->monitored_tasks().end();
}
Variant C:
1
2
3
4
5
6
7
8
9
10
bool is_monitored(const Monitor* m, const TaskId* t)
{
  assert (m);
  assert (t);
 
  auto it = std::find(m->monitored_tasks().begin(),
                      m->monitored_tasks().end(),
                      *t);
  return it != m->monitored_tasks().end();
}
Variant D:
1
2
3
4
5
6
7
8
9
10
bool is_monitored(const Monitor* m, const TaskId* t)
{
  if (m == nullptr) log_error("..."); // and continue
  if (t == nullptr) log_error("..."); // and continue
 
  auto it = std::find(m->monitored_tasks().begin(),
                      m->monitored_tasks().end(),
                      *t);
  return it != m->monitored_tasks().end();
}
Whatever your answer is, note that other developer’s choice may be different. If you are in disagreement with your colleague whether “defensive programming” is a good or an evil thing, it is possible that you both entirely agree on what is good and what is evil, and the only difference is in how you define “defensive programming”.
I do not intend to give my choice here. My only goal with this short post is to note that the term is ambiguous.