Skip to content

Instantly share code, notes, and snippets.

@jingzhehu
jingzhehu / const_qualifier.cpp
Last active June 13, 2018 20:20
Constant qualifier for variables and pointers
#include <iostream>
int main() {
// _1 type _2 * _3
// when const is in location _1 or_2, it means the type is const-qualified
// when const is in location _3, it means the ptr is const-qualified
// const_cast can modify the const-qualification of variable
// don't use const_cast unless you have to
//
// Created by Jingzhe Hu on 6/12/2018 Tuesday.
//
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
@jingzhehu
jingzhehu / bind_and_mem_fn.cpp
Last active June 13, 2018 20:20
Creating new callables: bind and mem_fn
#include <iostream>
using namespace std;
struct Foo {
void display_greetings() { cout << "Class Foo - greetings !" << endl; }
void display_number(int i) { cout << "Class Foo - the integer is: " << i << endl; }
int data = 7;
static const int static_data = 90;
};
@jingzhehu
jingzhehu / crtp.cpp
Last active July 1, 2018 23:08
CRTP: curiously recurring template pattern
#include <iostream>
template<typename T>
struct inequality {
bool operator!=(const T& that) {
return !(static_cast<const T&>(*this) == that);
}
};
// mutual dependence of inequality and point
@jingzhehu
jingzhehu / CMakeLists.txt
Created February 5, 2018 17:08
Make EOSIO cmake file compatible with CLion
# Add to the beginning of CMakeLists.txt
# JH edit
set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")
set(OPENSSL_LIBRARIES "/usr/local/opt/openssl/lib")
set(BINARYEN_ROOT "/usr/local/binaryen/")
set(BINARYEN_BIN "/usr/local/binaryen/bin/")
set(WASM_LLVM_CONFIG "/usr/local/wasm/bin/llvm-config")
@jingzhehu
jingzhehu / go_tour_ex_errors.go
Created April 24, 2017 15:04
A Tour of Go: Exercise Errors
package main
import (
"fmt"
)
func Sqrt(x float64) (float64, error) {
if x < 0 {
return 0, &ErrNegativeSqrt{x}
@jingzhehu
jingzhehu / go_tour_ex_stringers.go
Created April 24, 2017 13:57
A Tour of Go: Exercise Stringers
package main
import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
}
@jingzhehu
jingzhehu / go_tour_ex_fibonacci_closure.go
Created April 24, 2017 01:50
A Tour of Go: Exercise Fibonacci closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
a := 1
b := 1
c := 0
@jingzhehu
jingzhehu / go_tour_ex_loops_fns.go
Created April 23, 2017 23:11
A Tour of Go: Exercise Loops and Functions
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(1)
@jingzhehu
jingzhehu / go_tour_ex_slices.go
Created April 23, 2017 23:10
A Tour of Go - Exercise Slices
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for x := range pic {
pic[x] = make([]uint8, dx)
for y := range pic[x] {