C/C++ — Friends and Templates

November 3, 2007 by

In C++, if you declare a template class and you would like to have a friend function that has an argument of the current class type, you need to be careful when specifying the name of the template types. For example:

template <class T>
class A {

template <class T>
friend void b(A<T> a);
} ;

Will confuse the compiler, as you use the same template name, T, for the class template and the function template type. What you need to do, it to use a different name for the function template type:

template <class T>
class A {

template <class V>
friend void b(A<V> a);
} ;

Notice that I used T for the class template type and V for the function template type.

Unix/Linux — Redirect output with sudo

September 26, 2007 by

Sometimes you might need to redirect the output with sudo privileges. For example:

echo myhost.com > /etc/hostname

will give you the following error:

-bash: /etc/hostname: Permission denied

Unfortunately, using:

sudo echo myhost.com > /etc/hostname

will give the same error.

The solution is to start bash as sudo and then give the entire command to bash as input:

sudo bash -c “echo myhost.com > /etc/hostname”

Or as one reader suggested:

echo myhost.com | sudo tee /etc/hostname

C++ — Convert string to upper/lower case

August 4, 2007 by

To convert an std::string to upper case you can use the following:

#include <algorithm>
#include <string>

std::string data = “Abc”;
std::transform(data.begin(), data.end(),
data.begin(), ::toupper);

For converting to lower case, you need to replace ::toupper with ::tolower.

Other C/C++ not so frequently asked questions.

JavaScript — Trim white space

July 23, 2007 by

Here is a way to trim the white space from a string in Javascript:

replace(/^\s+|\s+$/g, ”)

By removing |\s+$ it will do a left trim only, and by removing ^\s+| it will do a right trim only (g is no longer necessary).

Unix/Linux – ICS printer name

May 26, 2007 by

To get a list with all the printers in ICS, ssh to printserver and run:

lpstat | grep Printer

Emacs — Search/Replace new line

March 24, 2007 by

In order to search/replace new line character (\n) in Emacs, instead of \n you need to type:

C-q C-j

C/C++ – _M_underflow_common

February 13, 2007 by

When you define you own character traits (e.g., ignorecase_traits – to ignore the case when comparing strings) you might run into the following link error:

In function `std::basic_filebuf<char, ignorecase_traits>::uflow()'::
undefined reference to `std::basic_filebuf<char, ignorecase_traits>::

The solution is to add the following to one of your CC/CPP files:

struct cGet_M_underflow_common : std::basic_filebuf<char> {
  int_type mGet_M_underflow_common(bool b) {
    return _M_underflow_common(b);

icfielbuf::int_type icfielbuf::_M_underflow_common(bool bump) {
  return reinterpret_cast<cGet_M_underflow_common *>(this)->

Emacs – change case

February 6, 2007 by

In order to change the case in Emacs you can use:

M + l to change to lowercase all the letters from the cursor to the end of the word
M + u same meaning, but upercase
M + c same meaning, but capitalizes

C/C++ – binary IO

February 1, 2007 by

Here is an example of how we can write to and read from files in binary:

ofstream file(“foo.txt”, ios::out | ios::binary);
unsigned e =101;
file.write(reinterpret_cast<char*>(&e), sizeof(unsigned));

ifstream file(“foo.txt”, ios::in | ios::binary);
unsigned e;
file.read(reinterpret_cast<char*>(&e), sizeof(unsigned));

More info:

Unix/Linux — Bash — String seperator

November 6, 2006 by

In Bash the default string separator is whitespace which means spaces, tabs, or blank lines. The string separator is used in various places, including when looping with for over a string. The string separator is stored in the IFS variable.

If you want to set the default separator to “:” you need to do:


If you want to set is to newline, you can do it like this:


‘\x0a’ is the hexadecimal notation for newline or Ctrl-J.

A common pattern is to save the old separator before changing and restore it when you are done:



or more simple, just unset it when you are done:


unset IFS