Saturday, May 2, 2015

C# Building a Dictionary in Attributes

Introduction

The last blog I did about .NET attributes was to add a simple attribute to an existing method. This entry is about building a "Dictionary" of data into a set of attributes on a method. For example, I want the attributes to document each parameter of a method. So what I want is something that looks like this:


[Method(Description="This is the fibonacci sequence"
       ,Parameters={ {"in", "The input parameter" } })]
int Fibonacci(int in)
{
    if (in == 0 || in == 1) return 1;
    return Fibonacci(in - 2) + Fibonacci(in - 1);
}

Keep in mind this is NOT valid syntax in .NET (at least I don't think it is).

Attribute Definition

The above description won't work- only because I don't think that C# allows parameter definitions with complex objects as values. However, I found a solution that is pretty good at [1]. The partial solution is to separate each element that would be in the dictionary into a unique attribute. The attribute I put together looks like this:
                                                                                                        
[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
public class SomeParameter : Attribute
{
     private string m_name;
     private string m_description;

     public string Name 
     {
         get
         {
             return m_name;
         }
         set
         {
             m_name = value;
         }
     }


     public string Description
     {
         get
         {
             return m_description;
         }
         set
         {
             m_description = value;
         }
     }
}

I put one of the critical sections in bold. The trick is to set the AllowMultiple flag to true, which lets us actually add more than one of these attributes to the method [2]. To use this:

[SomeParameter(Name="input1",Description="The input to fib.")]
public int Fibonacci(int input1)
{
}

Note: Remember to put the public in front of the method! If it is not listed as public, it will not show up when using the reflection method I'm going to show below. This isn't a very good example since fibonacci only takes one parameter. However if I add a new method:


[SomeParameter(Name="parm1",Description="The first input to Calculate2.")]
[SomeParameter(Name="parm2",Description="The second input to Calculate2.")]
public int Calculate2(int parm1, int parm2)
{
}

Why do this? The reason I want to do this is to add a way to query more information about parameters and functions. Here is my complete program to illustrate how the multiple parameters can also be queried:

using System;


[AttributeUsage(AttributeTargets.All)]
public class SomeMethod : Attribute
{
    private string m_description;

    public string Description
    {

        get
        {
            return m_description;
        }

        set
        {
            m_description = value;
        }
    }
}


[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
public class SomeParameter : Attribute
{
    private string m_name;
    private string m_description;

    public string Name
    {
        get
        {
            return m_name;
        }

        set
        {
            m_name = value;
        }
    }


    public string Description
    {
        get
        {
            return m_description;
        }

        set
        {
            m_description = value;
        }
    }


}


public class HelloWorld
{
    HelloWorld()
    {
    }


    [SomeMethod(Description="Sample calculation")]
    [SomeParameter(Name="parm1", Description="Parameter #1")]
    [SomeParameter(Name="parm2", Description="Parameter #2")]
    public int Calculate2(int parm1, int parm2)

    {
        return parm1 + parm2;
    }


    static public void Main()
    {
        HelloWorld h = new HelloWorld();

        int output = h.Calculate2(4, 5);


        Type hwInfo = typeof(HelloWorld);

        System.Reflection.MemberInfo[] memberInfo = hwInfo.GetMembers();
        foreach(System.Reflection.MemberInfo mInfo in memberInfo)
        {
            Console.WriteLine(mInfo.ToString());

            object[] attributes = mInfo.GetCustomAttributes(true);
            Console.WriteLine("\tNumber of attributes: " + attributes.Length);
            for(int i=0; i<attributes.Length; i++)
            {
                System.Console.WriteLine("\t\t" + attributes[i]);
            }
        }
    }
}

This program goes through and prints out all of the methods available in the C# class.


REFERENCES

[1] - http://stackoverflow.com/questions/21265431/how-can-i-accept-a-dictionary-of-data-as-a-custom-net-attribute-parameter

[2] - https://msdn.microsoft.com/en-us/library/tw5zxet9.aspx


Wednesday, April 29, 2015

Building a Custom Debian Kernel

Introduction

This entry is about building a custom Debian kernel. I've never done this before and I have some need for it. The instructions I'm basing this on are found at:

https://www.debian.org/releases/stable/i386/ch08s06.html.en

I'm not sure if this is a good place to start; it's what this article is based upon. You must have root privileges

What Kernel am I using?

The current kernel can be found simply by querying:

> uname -r

This returned back:

3.2.0-4-amd64

There are several ways to query the version of the kernel. I found them at the following website:

http://www.linfo.org/find_kernel_version.html

Command Line

> sudo apt-get install fakeroot
> sudo apt-get install  kernel-package
> sudo apt-get install linux-source-2.6
> sudo apt-get install ncurses-dev
> cd ~
> mkdir kernel
> cp /usr/src/linux-source-*.tar.bz2 .
> cd kernel
> make menuconfig
> make-kpkg clean
> fakeroot make-kpkg --initrd --revision=1.0 kernel_image
> sudo dpkg -i ../linux-image-3.2.65_1.0_amd64.deb
> sudo shutdown -r now


After a reboot, if you do the following you should now see the updated kernel version:


> uname -r

Now it returned back:

3.2.65

Interesting that it did not return back the architecture.

fakeroot

From what I can gather about fakeroot, its primary purpose is to allow non-root users a way to produce files that have "root permissions/ownership" [3]. Essentially it gets rid of the requirement that the developer building the kernel needs to be root to build files with root ownership.

kernel-package

If make-kpkg is missing then kernel-package has not been installed.

linux-source-2.6

This is the source code for the kernel. When installing this package, it will pull down the source code into /usr/src/linux-source-X.X.tar.bz2. The sequence of commands above shows that we copy the tarball into a local directory called "kernel".

make menuconfig

This step is pretty cool because it allows the developer to customize what features to enable/disable and which files should be built in versus built into modules. Doing it this way requires that ncurses be installed (see the installation lines for details). It's a pretty self-explanatory user interface.


REFERENCES

[1] https://www.debian.org/releases/stable/i386/ch08s06.html.en

[2] http://www.linfo.org/find_kernel_version.html

[3] http://unix.stackexchange.com/questions/9714/what-is-the-need-for-fakeroot-command-in-linux


Sunday, March 8, 2015

C# .NET Attributes Data (Beginner's Guide)

Introduction

This guide is all about how to add metadata to a C# program. This entry is aimed at the beginning C# developer who has experience with other programming languages. Metadata has a variety of uses- at the company I work for we're going to use it to provide more verbose information to consumers of some given code. It's a very .NET-y solution to a problem we've already solved in C++ using parsers. The advantage to metadata is that it's a solution that is already supported by the .NET framework.

Please note that all of the code that I'm writing is compiled using Mono. Mono is a cross-platform tool that can compile C# code into an intermediate form which can then be run.

The Initial Program

For the initial program I decided to use the "Hello World" program provided by the Mono project [1]. This had the duel purpose of testing out my Mono installation. The original HW program was kind of simple for the type of meta-information I wanted to provide. So it was modified. Here is the original code (once again, credit to the mono project):

using System;

public class HelloWorld
{
    static public void Main()
    {
        Console.WriteLine("Hello Mono World!");
    }
}

Let's change the program to instantiate a HelloWorld object and use a method to print a message out.

using System;

public class HelloWorld
{
    public void PrintMsg(String msg)
    {
        Console.WriteLine(msg);
    }

    static public void Main()
    {
        HelloWorld hw = new HelloWorld();
        hw.PrintMsg("Hello World!");
    }
}

Adding an Attribute Class

The example class now has a basic method (PrintMsg). I want to start inserting special meta data to the class and some of its functions. Let's start with a simple description field. To achieve this a custom Attribute class must be written. I'm going to call the attribute class "SomeMethod":

using System;

[AttributeUsage(AttributeTargets.All)]
public class SomeMethod : Attribute
{
    private string m_description;

    public string Description
    {
        get
        {
            return m_description;
        }
        set 
        {
            m_description = value;
        }
    }
}

This is a really simple attributes class that has one property called "Description". The Description property is implemented with the get/set methods. These are just shorthand ways to implement these basic accessors. It's also much more formal than most other languages.

The top line contains [AttributeUsage(AttributeTargets.All)] seems to help declare this class as an attribute that can be applied using metadata calls [2].

Applying the Custom Attribute

To apply the custom attribute, all that is necessary is to apply some metadata before the method and it automatically gets attached:

using System;

[AttributeUsage(AttributeTargets.All)]
public class SomeMethod : Attribute
{
    private string m_description;

    public string Description
    {
        get
        {
            return m_description;
        }
        set 
        {
            m_description = value;
        }
    }
}


public class HelloWorld
{
    [SomeMethod(Description="This is a description of PrintMsg")]
    public void PrintMsg(String msg)
    {
        Console.WriteLine(msg);
    }

    static public void Main()
    {
        HelloWorld hw = new HelloWorld();
        hw.PrintMsg("Hello World!");
    }
}

I put the introduced attribute in bold.

Using Reflection to Query Attributes

The only step left is now querying the attributes placed on members/methods. This is pretty simple using a language feature called Reflection. So I modified the main part of the program to contain the following lines:

public class HelloWorld
{
    [SomeMethod(Description="This is a description of PrintMsg")]
    public void PrintMsg(String msg)
    {
        Console.WriteLine(msg);
    }

    static public void Main()
    {
        HelloWorld hw = new HelloWorld();
        hw.PrintMsg("Hello World!");

        Type hwInfo = typeof(HelloWorld);

        System.Reflection.MemberInfo[] memberInfo = hwInfo.GetMembers();
        foreach(System.Reflection.MemberInfo mInfo in memberInfo)
        {
            Console.WriteLine(mInfo.ToString());
       
            object[] attributes = mInfo.GetCustomAttributes(true);
            Console.WriteLine("\tNumber of attributes: " + attributes.Length);
            for(int i=0; i<attributes.Length; i++)
            {
                System.Console.WriteLine("\t\t" + attributes[i]);
            }
        }
    }
}

This program is now modified to print out all of the methods inside of HelloWorld and whether or not there were custom attributes applied. The point of this step is to partially explore what can be queried with Reflection.

Learning Note: When I ran this program in Mono it was observed that my one simple class had more than one method. Being a veteran C++ developer I am familiar with methods (constructors, assignment operators, etc) being added to classes. It appears as though C# also does something similar adding: 

  • Boolean Equals(System.String)
  • Int32 GetHashCode()
  • System.Type GetType()
  • System.String ToString()
  • Void .ctor()
I must admit that these methods all look really useful! As a C# beginner I think another entry might be useful to describe what these methods do. 


If we want to detect when a method has an attribute, it's as simple as:

public class HelloWorld
{
    [SomeMethod(Description="This is a description of PrintMsg")]
    public void PrintMsg(String msg)
    {
        Console.WriteLine(msg);
    }

    static public void Main()
    {
        HelloWorld hw = new HelloWorld();
        hw.PrintMsg("Hello World!");

        Type hwInfo = typeof(HelloWorld);

        System.Reflection.MemberInfo[] memberInfo = hwInfo.GetMembers();
        foreach(System.Reflection.MemberInfo mInfo in memberInfo)
        {
            Console.WriteLine(mInfo.ToString());

            SomeMethod sm = 
               (SomeMethod) Attribute.GetCustomAttribute( mInfo
                                                        , typeof(SomeMethod));
            if (sm != null)
            {
                Console.WriteLine("\tHAS IT!\n");
            }
        }
    }
}

This program will now print out a message when the SomeMethod is obtained!

Conclusion

I really hate to admit this- C# looks awesome! I really enjoyed using Mono to run these examples. I know the examples were simple but I'm impressed with how the language defines its own custom attributes. In my next entry on this feature I'll explore how to add lists of things to attributes.


REFERENCES

[1] - http://www.mono-project.com/docs/getting-started/mono-basics/

[2] - https://msdn.microsoft.com/en-us/library/aa288454%28v=vs.71%29.aspx

[3] -https://msdn.microsoft.com/en-us/library/aa288454%28v=vs.71%29.aspx#vcwlkattributestutorialanchor3


Sunday, March 1, 2015

Where to find Qt Configure Flags

Introduction

It's Sunday afternoon and I'm about to re-setup a development virtual machine. In the creation of it I'm going to be building Qt5 from source on Linux. Building from source can be troublesome at times because it's hard to find good information about the configure "script". This small entry is about where to find information (from source code) about Qt's configure "script".

If I do a quick google search with "Qt configure documentation", the first good link I get is for Qt4:

http://qt-project.org/doc/qt-4.8/configure-options.html

Not bad. The link below it is for Qt 5.4:

http://doc.qt.io/qt-5/configure-options.html

In the tradition of ever obtrusive documentation, it's very wordy and hard to find what you are looking for. In my opinion the documentation style for Qt5 is inferior to Qt4 (opinions are subject to change).

This entry is all about the actual mechanism behind the configure "script". I am putting script in quotation marks because it's not actually a script- it's an executable. The first step after pulling Qt down from source is to run "configure" in the root directory. Obviously if using Visual Studio you must have run this from the VS Command Prompt.

QT/configure [.bat]

There are two scripts in two languages named "configure" in the root directory. The regular old configure script is a shell script that simply executes qtbase/configure. The configure.bat does the same in its own way executing qtbase/configure.bat.

QT/qtbase/configure.bat

This script begins the "bootstrapping configure". Essentially it runs a perl script called syncqt.pl and then tries to build the configure tool in QT/tools/configure. The configure executable tool takes the place of the QT/qtbase/configure. When I built Qt 5.3 it looks as if this tool was never built- looks as if this is a Windows only step.

QT/qtbase/configure

This is a shell script that does the actual setup for the build. When using Visual Studio, this won't be utilized. Instead the configure application listed above is used. The reason why it exists this way is probably due to batch files being rotten but I could be wrong.

Where are the build options for configure?

In conclusion, to find the base options for the configure step in Qt the developer could look in QT/qtbase/configure (which is the script used for Linux) OR look qt QT/qtbase/tools/configureapp.cpp in the method Configure::parseCmdLine().

 - Visual Studio     - SOURCE : $QTDIR/qtbase/tools/configure/configureapp.cpp
   - TYPE   : Binary Application
   - METHOD : Configure::parseCmdLine()
 - Linux (Other)
   - SOURCE : $QTDIR/qtbase/configure
   - TYPE   : Shell Script
   - METHOD : Line 828? Look for "parse command line arguments"

Using the source code the developer can get definitive information about the configure arguments that can be passed into building Qt.




Saturday, January 31, 2015

QQuickItem (Cannot assign object to list)

Introduction

I find myself with a problem that the Qt forums can't solve. The only way to fix it is to dive into the Qt source code and figure it out myself. This blog post is all about the internals of QtQuick2 and is meant for an intermediate audience. I apologize about this blogpost as it's mainly about how I'm fixing this one issue; but I assure the reader that once finished they'll have more intimate knowledge of QtQuick2.

Prerequisites for this entry include basic knowledge about QQuickItem and customization using C++. Before writing this article I was familiar with the class predecessor, QDeclarativeItem. The two are very similar yet very very different. That might be the subject of another post.

The first section is all about detailing the crime scene or in other words describing the problem. The second section documents some useful information about import's in Qt5/QtQuick2 (including some really general information about qmldir). The third section after that documents the "default property" in QML and what it is all about.The fourth section is all about where and how the definitions of QQuickItems are stored.

Problem Description

During a port of QtQuick1 to QtQuick2 code I got the error:

types.qml: 48:1: Cannot assign object to list

We've all been there- a problem we're seeing in a third party library. This is a kind of problem that no one else sees. We didn't want to deal with branching our code for the two distributions. Using some typedefs along with a hand rolled preprocessor the whole thing works great. Except when I saw the following error when loading some QML:

types.qml: 48:1: Cannot assign object to list

This was a strange error indeed. My QML program looked like this:

import QtQuick 2.0
import blah 1.0

TypesCreator {
    id: root
    ...

    Connections { // Line 48
        target: __start__ // __start__ is an object I bound a QObject to.
        onStart: {
            // Do something.
        }
    }
}

The TypesCreator element is part of the blah import that is custom to the application. In the interest of treating this article as a "whodunnit" type of story you can assume that TypesCreator's object looks like this in C++:

class TypesCreator : public QQuickItem
{
    Q_OBJECT
    public:

    private:
};

This obviously does nothing except acts as a container for other objects. In any crime scene you have to know the area. In this case I needed to know what was imported and what wasn't.

Where/What Modules Are/Exist?

The first theory I had was that the Connections object wasn't registered. Eventually it was read that Connections was technically not part of QtQuick 2 but rather was part of QtQml [1]. Upon further reading, QtQml components are automatically included as part of QtQuick. This theory is a dead end- however it led me to trying to solve the riddle of "what objects belong to what import"?

The answer is:

http://doc.qt.io/qt-5/modules-qml.html

As a sidenote to this section: the Qt5 documentation is quite irritating. While being much more comprehensible it fails to be searchable. It took me over 5 minutes to find the module list.

The module source code for the imports is organized under:

qtdeclarative/src/imports/

The easiest way to decipher the imports is to go under a directory and read the qmldir file. The qmldir file has all of the information about what types of classes for QML the module contains. For example I'm looking at:

qtdeclarative/src/imports/qtquick2/qmldir

This is what it reads:

module QtQuick
plugin qtquick2plugin
classname QtQuick2Plugin
typeinfo plugins.qmltypes
designersupported

Essentially it is a hacked together file format that attaches some extra information onto a module. In this case the first line defines what the module name is. The second line tells me that this is a collection of C++ objects that make up this module. The rest I'll skip for now- I just want to know where the basic information is at this point.

Default QML Properties

One of the biggest questions I had when trying to solve this problem was what property could this not assign an object to? I put that in bold mainly to get back to the original error message:

types.qml: 48:1: Cannot assign object to list

So what was this property? The only way I could actually solve this was to get a breakpoint into the code that was printing the error message and then work backwards. To achieve this purpose I:
  1. Performed a grep to find the string "Cannot assign object to list" in the Qt Source code. It turned up in qtdeclarative/src/qml/compiler/qqmltypecompiler.cpp.
  2. When I found the message I noticed that it passed in a propertyName as a parameter to the function, I outputted this using qDebug(). 
  3. Finally to really throw a wrench into things I throw a standard exception. This allows Visual Studio to trip up each time it encounters this error.
When running I discovered the property name being set was "data". This was confusing since no where in my code did a "data" object exist. That's not true though! Inside of the source code for QQuickItem it defines the property "data" as:

Q_PRIVATE_PROPERTY(QQuickItem::d_func(), QQmlListProperty<QObject> data READ data DESIGNABLE false)

So every item in QML has a data property and apparently it's a list of QObject's. That's something new- however I'm never using the "data" property. As I was about to find out, this is also not true.

There's a feature in QML where the developer can specify the "default property" [3]. Inside of QQuickItem it has the following macro also added in:

Q_CLASSINFO("DefaultProperty", "data")

This means that any sort of unbound object created will be added to "data". Since every QML object has the default property set to "data" all of what would appear to be the child items are added in via the data property. It is mentioned in the documentation that the data elements when added are also added to the children list.

This explains a lot; it explains why the property it cannot add Connections to is called "data", it explains why the type it is trying to add to is referenced as a list. The only thing it doesn't solve is why Connections isn't registered as a QObject. It should be; something isn't adding up. The next logical step is to observe how the Connections object is added to a QML Engine.

The How and Where QML Types Are Stored

The final bit of the puzzle for this entry is determining why the QML types cannot be found. This involved stepping into Qt. I found out the following:

1) For each type defined for QML, there is a QQmlType object. According to my notes the declaration can be found in qqmlmetatype_p.h. [4]
2) There's an object of class QQmlMetaTypeData that contains a map of all the created types. When the developer calls qmlRegisterType, the type created, its name, and version information all end up in a map here.The declaration is found in qqmlmetatype.cpp [5].
3) The map itself holds a hash of type identifier to QQmlType (line 76 in the source).

My best guess is that the type identifier for a well known type is getting overwritten in this map. The basic type I'm trying to use was replaced with some garbage type. To understand this, I tracked down where the QML numeric type was defined. If walking up the call stack, the QML Type identifier is found to map to the QMetaTypeIdHelper<T>::qt_metatype_id() where T is the type of the class.

The summary to this section is that any time a registration occurs with the meta type system the identifier that is produced is also used in QML to map type information. The numbers are mostly guaranteed to be unique since they are using a basic atomic integer mechanism so it makes sense. Having this information in hand I could now surmise that somewhere I was doing something stupid when registering types. This guess proved to be right since I found this little gremlin in the code:

qRegisterMetaType<QObject*>("LayerItem");

Looking at it now I'm kicking myself. This was using the type id from QObject* to map to the string "LayerItem".

Conclusion

While searching for why the runtime error "Cannot assign object to list" in QML I learned a lot about how the language parses, interprets, and stores its programs:

  • Modules in QtQuick2 are very similar to QtQuick1 modules with the exception they can store more meta information in qmldir.
  • QObject based classes have the ability to store a default parameter which can be used to store "child" information in a custom way.
  • QML Types are stored in a map inside of QQmlMetaTypeData. The id's used in the map come from the Qt Meta Object System.
In the process of learning about all of these facets of the engine I found the solution to my problem which was to get rid of a call to qRegisterMetaType<QObject*>. 


REFERENCES

[1] - http://doc.qt.io/qt-5/qtqml-qmlmodule.html

[2] - http://qt-project.org/doc/qt-4.8/declarative-cppextensions-referenceexamples-default.html

[3] - http://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#property-attributes

[4] - https://qt.gitorious.org/qt/qtdeclarative/source/ccd69247eaa6289bc31115f863ba4737b14fe34e:src/qml/qml/qqmlmetatype_p.h

[5] - https://qt.gitorious.org/qt/qtdeclarative/source/e2ea0a83cc876fb54a2a8bf6f1350dbfa52f596b:src/qml/qml/qqmlmetatype.cpp


Thursday, January 1, 2015

Standard Remove Functions

The subject of this blog entry is removing elements from containers in C++. When it comes to removing elements I either 1) have to look up how to do removals or 2) I handwrite the loop and feel guilty all day. This entry focuses most specifically on the std::remove family of functions and what they actually do.

What I Know

I know that in a standard vector, erase is the weapon of choice for removing elements from a container. Erase is of course only one part of the equation; the standard library also has a function that does removals. At this point I know of the "erase-remove" idiom [1] but am unsure of how to apply it.

In the case of associative containers that return back constant iterators the erase-remove idiom does not apply (see [1] for details). It makes sense because std::remove actually reorders the elements in the container.

I want to make this next assertion in bold: remove/remove_if do NOT resize the containers and do not necessarily erase values from a container. That statement is the key to understanding what remove actually does.

The Remove Part of Erase-Remove

Let's start with an example program to clearly explain what's going on with std::remove:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main(int argc, char** argv)
{
    std::vector<int> v = { 8, 3, 5, 7, 9, 2, 4, 1 };
    
    std::copy( v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::remove(v.begin(), v.end(), 9);

    std::copy( v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
}

What I thought would happen would be that 9 moves to the end of the container. So the output I thought to expect was:

8 3 5 7 9 2 4 1
8 3 5 7 2 4 1 9

This is NOT what I observed when I ran the program. Instead what I got was:

8 3 5 7 9 2 4 1
8 3 5 7 2 4 1 1

That makes more sense. Why copy 9 to the end of the container if it's just going to be removed anyways? It's an efficiency thing. If that's the case, why not just have remove simply get rid of the elements? Again- it's more efficient to remove the elements then erase all of items in bulk at the end of a container.

I guess the moral of this part of the entry is that don't count on remove keeping the value of whatever it is you are removing- as is illustrated with this simple program it does the remove (as advertised) but does NOT move it to the end of the container! This may matter if you are using a container of shared pointers.

Remove-If

Often times when I'm writing code to remove elements from a container it's not as simple as "remove this exact item from the container". Most of the time it involves some sort of predicate case. That's where remove_if comes into play. With the addition of lambda expressions it's even more addictive to use:


#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main(int argc, char** argv)
{
    std::vector<int> v = { 8, 3, 5, 7, 9, 2, 4, 1 };
    
    std::copy( v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::remove(v.begin(), v.end(),[](int n) -> bool { return (n % 2 == 1); });

    std::copy( v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
}
The example above will remove any odd number from the container. From the remove example above, I'd expect the output of my program now to be:

8 3 5 7 9 2 4 1
8 2 4 7 9 1 1 1

The reason I think this is the container is because it moves all of the elements with the non-matching criteria (i.e. the elements to NOT be removed) to the front of the container. Anything else in the back is irrelevant. Therefore I move all the non-matches to the front in order (8, 2, 4). At this point I have no idea what is at the end of the container. The real answer is actually:

8 3 5 7 9 2 4 1
8 2 4 7 9 2 4 1
This makes sense. Why would it put 1's at the end? It just left the good values in because remove_if doesn't give a flying whatever as to what is removed.

Remove Copy

Okay, what the hell is this one? The remove_copy function is weird because it is meant to be used differently than remove/remove_if. It is so different than remove/remove_if I've made the subheading red. Instead of removing from within a container it copies the preserved values to an output iterator. Here's an example:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <vector>

int main(int argc, char** argv)
{
    std::vector<int> v = { 8, 3, 5, 7, 9, 2, 4, 1 };
    std::list<int> outputs;

    std::copy( v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::remove_copy(v.begin(), v.end(), std::back_inserter(outputs), 5);

    std::copy( v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::copy( outputs.begin(), outputs.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
}
One more time let's play the game of "Expected Outputs". I would have expected:

8 3 5 7 9 2 4 1
8 3 7 9 2 4 1 1
8 3 7 9 2 4 1

Once again I was wrong- the original vector is no longer modified! So the correct answer is:

8 3 5 7 9 2 4 1
8 3 5 7 9 2 4 1
8 3 7 9 2 4 1

Wow. The other thing that this example is missing is that the iterator that is returned from the remove_copy does not actually point at the container that is passed in. This makes sense because the container passed in is never altered. Instead the iterator returned is an output iterator that points at the end of the output container [2]. Did not see that coming.

Almost wish I could have attended the meeting where they decided to add remove_copy. It's a strange addition and breaks with how remove and remove_if are used. It's got great utility and I'd almost prefer using remove_copy in most code that doesn't have to go fast since it is a bit more understandable (i.e. doesn't need an additional erase step).

REFERENCES

[1] - http://en.wikipedia.org/wiki/Erase-remove_idiom

[2] - http://www.cplusplus.com/reference/algorithm/remove_copy/