C++ Template Return Type Deduction from Integer Parameter

#include <iostream>

template<int N>
struct item_return{ typedef int type; };
 
template<int T>
typename item_return<T>::type foo(){ return T; }
 
template<>
struct item_return<1>{ typedef int type; };

template<>
struct item_return<2>{ typedef double type; };
 
template<>
int foo<1>(){ return 42; }

template<>
double foo<2>(){ return 42.1; }
 
int main(){
    std::cout << foo<2>() << std::endl;
}

Live Demo:

https://wandbox.org/permlink/bmRsHB8ICdUWd7Mi

Read More

Hungarian Notation

When I started programming in C/C++, one particular variable naming convention, Hungarian notation, is pretty popular. Even nowadays it is still being used. But based my experience, I found out that it is very annoying. Here are some reasons I am against it:

Read More

Install Nvidia CUDA 8 and cuDNN 6

Many deep learning libraries use Nvidia GPU to accelerate the computation. The CUDA Toolkit needs to install to make use of the GPU. The NVIDIA CUDA Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks which is worth installing. Current version of CUDA is 9 and current version of cuDNN is 7. But many deep learning libraries have yet to upgrade to the current versions of CUDA and cuDNN. So here I present a way to install CUDA 8 and cuDNN 6.

Read More

Disqus comments not showing up

I use Disqus comment system. One thing puzzled me is that the comments did not show up sometimes, but sometimes it did. Finally I figured out it is because github pages treats “http” and “https” requests as the same. But Disqus treats them as two different URLs. So the comments are not visible to each other.

Read More

Show Html files in Github

In the last post, I want to show the html file hosted in github, but instead html source code is displayed. The solution is to create a gh-pages branch in your project:

Read More

Display image, show mouse position and pixel values from web browser

If you want to view the images, and want to know the mouse location and pixel values, you may install some image viewer(s). I am at the same boat. After some research, I found that there is no light-weighted image viewer which can show the pixel values at Linux platform up to my best knowledge. So I decide to write one by myself. It turns out web browser + Javascript are good enough to do this. Here is the Javascript based image viewer:

Read More